<!DOCTYPE html>
<html>
<head>
    

    

    



    <meta charset="utf-8">
    
    
    
    
    <title>千峰教程笔记 | SongKaBaKa</title>
    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
    
    <meta name="theme-color" content="#3F51B5">
    
    
    <meta name="keywords" content="学习">
    <meta name="description" content="input获得的数据都是字符串型，所以要补充一些类型转换函数 布尔型数字非0即是True 字符串非None #列表类型names &#x3D;[‘姚万万’， ‘邹碧慧’，’郑美水’，’蔡顺利’，’李想’， ‘蔡坤’] 空列表是 [ ] #字典类型person &#x3D; {‘name’: ‘ 袁奋’，’age’: 18，’addr’: ， 湖北’，’ 身高’: ‘180cm’} 空字典是 {} #元组类型nums&#x3D;">
<meta property="og:type" content="article">
<meta property="og:title" content="千峰教程笔记">
<meta property="og:url" content="https://gitee.com/songbug/blog.git/2020/07/16/%E5%8D%83%E5%B3%B0%E6%95%99%E7%A8%8B%E7%AC%94%E8%AE%B0/index.html">
<meta property="og:site_name" content="SongKaBaKa">
<meta property="og:description" content="input获得的数据都是字符串型，所以要补充一些类型转换函数 布尔型数字非0即是True 字符串非None #列表类型names &#x3D;[‘姚万万’， ‘邹碧慧’，’郑美水’，’蔡顺利’，’李想’， ‘蔡坤’] 空列表是 [ ] #字典类型person &#x3D; {‘name’: ‘ 袁奋’，’age’: 18，’addr’: ， 湖北’，’ 身高’: ‘180cm’} 空字典是 {} #元组类型nums&#x3D;">
<meta property="og:locale" content="en_US">
<meta property="article:published_time" content="2020-07-16T07:20:36.000Z">
<meta property="article:modified_time" content="2020-08-28T01:36:37.585Z">
<meta property="article:author" content="Song Wani">
<meta property="article:tag" content="学习">
<meta name="twitter:card" content="summary">
    
    <link rel="shortcut icon" href="/blog/favicon.ico">
    <link rel="stylesheet" href="//unpkg.com/hexo-theme-material-indigo@latest/css/style.css">
    <script>window.lazyScripts=[]</script>

    <!-- custom head -->
    

<meta name="generator" content="Hexo 4.2.1"></head>

<body>
    <div id="loading" class="active"></div>

    <aside id="menu" class="hide" >
  <div class="inner flex-row-vertical">
    <a href="javascript:;" class="header-icon waves-effect waves-circle waves-light" id="menu-off">
        <i class="icon icon-lg icon-close"></i>
    </a>
    <div class="brand-wrap" style="background-image:url(/blog/img/brand.jpg)">
      <div class="brand">
        <a href="/blog/" class="avatar waves-effect waves-circle waves-light">
          <img src="/blog/img/avatar.jpg">
        </a>
        <hgroup class="introduce">
          <h5 class="nickname">Song Wani</h5>
          <a href="mailto:bugsong@wo.cn" title="bugsong@wo.cn" class="mail">bugsong@wo.cn</a>
        </hgroup>
      </div>
    </div>
    <div class="scroll-wrap flex-col">
      <ul class="nav">
        
            <li class="waves-block waves-effect">
              <a href="/blog/"  >
                <i class="icon icon-lg icon-home"></i>
                主页
              </a>
            </li>
        
            <li class="waves-block waves-effect">
              <a href="/blog/archives"  >
                <i class="icon icon-lg icon-archives"></i>
                Archives
              </a>
            </li>
        
            <li class="waves-block waves-effect">
              <a href="/blog/tags"  >
                <i class="icon icon-lg icon-tags"></i>
                Tags
              </a>
            </li>
        
            <li class="waves-block waves-effect">
              <a href="/blog/categories"  >
                <i class="icon icon-lg icon-th-list"></i>
                Categories
              </a>
            </li>
        
            <li class="waves-block waves-effect">
              <a href="https://songbug.gitee.io/blog" target="_blank" >
                <i class="icon icon-lg icon-github"></i>
                Github
              </a>
            </li>
        
      </ul>
    </div>
  </div>
</aside>

    <main id="main">
        <header class="top-header" id="header">
    <div class="flex-row">
        <a href="javascript:;" class="header-icon waves-effect waves-circle waves-light on" id="menu-toggle">
          <i class="icon icon-lg icon-navicon"></i>
        </a>
        <div class="flex-col header-title ellipsis">千峰教程笔记</div>
        
        <div class="search-wrap" id="search-wrap">
            <a href="javascript:;" class="header-icon waves-effect waves-circle waves-light" id="back">
                <i class="icon icon-lg icon-chevron-left"></i>
            </a>
            <input type="text" id="key" class="search-input" autocomplete="off" placeholder="Search">
            <a href="javascript:;" class="header-icon waves-effect waves-circle waves-light" id="search">
                <i class="icon icon-lg icon-search"></i>
            </a>
        </div>
        
        
        <a href="javascript:;" class="header-icon waves-effect waves-circle waves-light" id="menuShare">
            <i class="icon icon-lg icon-share-alt"></i>
        </a>
        
    </div>
</header>
<header class="content-header post-header">

    <div class="container fade-scale">
        <h1 class="title">千峰教程笔记</h1>
        <h5 class="subtitle">
            
                <time datetime="2020-07-16T07:20:36.000Z" itemprop="datePublished" class="page-time">
  2020-07-16
</time>


	<ul class="article-category-list"><li class="article-category-list-item"><a class="article-category-list-link" href="/blog/categories/%E7%BC%96%E7%A8%8B/">编程</a></li></ul>

            
        </h5>
    </div>

    


</header>


<div class="container body-wrap">
    
    <aside class="post-widget">
        <nav class="post-toc-wrap post-toc-shrink" id="post-toc">
            <h4>TOC</h4>
            <ol class="post-toc"><li class="post-toc-item post-toc-level-3"><a class="post-toc-link" href="#布尔型"><span class="post-toc-number">1.</span> <span class="post-toc-text">布尔型</span></a></li><li class="post-toc-item post-toc-level-3"><a class="post-toc-link" href="#列表类型"><span class="post-toc-number">2.</span> <span class="post-toc-text">#列表类型</span></a></li><li class="post-toc-item post-toc-level-3"><a class="post-toc-link" href="#字典类型"><span class="post-toc-number">3.</span> <span class="post-toc-text">#字典类型</span></a></li><li class="post-toc-item post-toc-level-3"><a class="post-toc-link" href="#元组类型"><span class="post-toc-number">4.</span> <span class="post-toc-text">#元组类型</span></a></li><li class="post-toc-item post-toc-level-3"><a class="post-toc-link" href="#集合类型"><span class="post-toc-number">5.</span> <span class="post-toc-text">#集合类型</span></a></li></ol></li><li class="post-toc-item post-toc-level-2"><a class="post-toc-link" href="#类型的查看"><span class="post-toc-number"></span> <span class="post-toc-text">类型的查看</span></a></li><li class="post-toc-item post-toc-level-2"><a class="post-toc-link" href="#不同进制数的表示以及转换"><span class="post-toc-number"></span> <span class="post-toc-text">不同进制数的表示以及转换</span></a></li><li class="post-toc-item post-toc-level-2"><a class="post-toc-link" href="#类型转换函数-应该是称为内置类"><span class="post-toc-number"></span> <span class="post-toc-text">类型转换函数(应该是称为内置类)</span></a></li><li class="post-toc-item post-toc-level-2"><a class="post-toc-link" href="#将数据转换为布尔型"><span class="post-toc-number"></span> <span class="post-toc-text">将数据转换为布尔型</span></a></li><li class="post-toc-item post-toc-level-2"><a class="post-toc-link" href="#比较运算"><span class="post-toc-number"></span> <span class="post-toc-text">比较运算</span></a></li><li class="post-toc-item post-toc-level-2"><a class="post-toc-link" href="#算术运算符"><span class="post-toc-number"></span> <span class="post-toc-text">算术运算符</span></a><ol class="post-toc-child"><li class="post-toc-item post-toc-level-5"><a class="post-toc-link" href="#字符串里有限度的支持加法和乘法运算"><span class="post-toc-number">0.0.1.</span> <span class="post-toc-text">字符串里有限度的支持加法和乘法运算</span></a></li></ol></li></ol></li></ol></li><li class="post-toc-item post-toc-level-2"><a class="post-toc-link" href="#赋值运算符"><span class="post-toc-number"></span> <span class="post-toc-text">赋值运算符</span></a><ol class="post-toc-child"><li class="post-toc-item post-toc-level-3"><a class="post-toc-link" href="#传递赋值-连续赋值"><span class="post-toc-number">1.</span> <span class="post-toc-text">传递赋值(连续赋值)</span></a></li></ol></li><li class="post-toc-item post-toc-level-2"><a class="post-toc-link" href="#比较运算符"><span class="post-toc-number"></span> <span class="post-toc-text">比较运算符</span></a></li><li class="post-toc-item post-toc-level-2"><a class="post-toc-link" href="#逻辑运算符"><span class="post-toc-number"></span> <span class="post-toc-text">逻辑运算符</span></a></li><li class="post-toc-item post-toc-level-2"><a class="post-toc-link" href="#位运算符"><span class="post-toc-number"></span> <span class="post-toc-text">位运算符</span></a></li><li class="post-toc-item post-toc-level-2"><a class="post-toc-link" href="#逻辑运算符的优先级"><span class="post-toc-number"></span> <span class="post-toc-text">逻辑运算符的优先级</span></a></li><li class="post-toc-item post-toc-level-2"><a class="post-toc-link" href="#分支语句"><span class="post-toc-number"></span> <span class="post-toc-text">分支语句</span></a><ol class="post-toc-child"><li class="post-toc-item post-toc-level-4"><a class="post-toc-link" href="#小结："><span class="post-toc-number">0.1.</span> <span class="post-toc-text">小结：</span></a></li></ol></li></ol></li><li class="post-toc-item post-toc-level-2"><a class="post-toc-link" href="#循环语句"><span class="post-toc-number"></span> <span class="post-toc-text">循环语句</span></a><ol class="post-toc-child"><li class="post-toc-item post-toc-level-4"><a class="post-toc-link" href="#while循环"><span class="post-toc-number">0.1.</span> <span class="post-toc-text">while循环</span></a></li><li class="post-toc-item post-toc-level-4"><a class="post-toc-link" href="#for循环"><span class="post-toc-number">0.2.</span> <span class="post-toc-text">for循环</span></a></li><li class="post-toc-item post-toc-level-4"><a class="post-toc-link" href="#break与continue"><span class="post-toc-number">0.3.</span> <span class="post-toc-text">break与continue</span></a></li></ol></li></ol></li><li class="post-toc-item post-toc-level-2"><a class="post-toc-link" href="#字符串"><span class="post-toc-number"></span> <span class="post-toc-text">字符串</span></a><ol class="post-toc-child"><li class="post-toc-item post-toc-level-3"><a class="post-toc-link" href="#字符串的表示方式"><span class="post-toc-number">1.</span> <span class="post-toc-text">字符串的表示方式</span></a></li><li class="post-toc-item post-toc-level-3"><a class="post-toc-link" href="#字符串的下标和切片"><span class="post-toc-number">2.</span> <span class="post-toc-text">字符串的下标和切片</span></a><ol class="post-toc-child"><li class="post-toc-item post-toc-level-4"><a class="post-toc-link" href="#下标又称为索引"><span class="post-toc-number">2.1.</span> <span class="post-toc-text">下标又称为索引</span></a></li><li class="post-toc-item post-toc-level-4"><a class="post-toc-link" href="#切片为赋值一部分内容生成一个新的内容"><span class="post-toc-number">2.2.</span> <span class="post-toc-text">切片为赋值一部分内容生成一个新的内容</span></a></li></ol></li><li class="post-toc-item post-toc-level-3"><a class="post-toc-link" href="#字符串常见的操作"><span class="post-toc-number">3.</span> <span class="post-toc-text">字符串常见的操作</span></a><ol class="post-toc-child"><li class="post-toc-item post-toc-level-4"><a class="post-toc-link" href="#查找内容相关方法："><span class="post-toc-number">3.1.</span> <span class="post-toc-text">查找内容相关方法：</span></a></li><li class="post-toc-item post-toc-level-4"><a class="post-toc-link" href="#判断相关方法："><span class="post-toc-number">3.2.</span> <span class="post-toc-text">判断相关方法：</span></a></li><li class="post-toc-item post-toc-level-4"><a class="post-toc-link" href="#replace-方法"><span class="post-toc-number">3.3.</span> <span class="post-toc-text">replace()方法</span></a></li><li class="post-toc-item post-toc-level-4"><a class="post-toc-link" href="#内容分隔相关操作-带r的与不带r的好像就是从哪边开始的意思"><span class="post-toc-number">3.4.</span> <span class="post-toc-text">内容分隔相关操作(带r的与不带r的好像就是从哪边开始的意思)</span></a></li><li class="post-toc-item post-toc-level-4"><a class="post-toc-link" href="#Pycharm使用建议"><span class="post-toc-number">3.5.</span> <span class="post-toc-text">Pycharm使用建议</span></a></li><li class="post-toc-item post-toc-level-4"><a class="post-toc-link" href="#Pycharm快捷键"><span class="post-toc-number">3.6.</span> <span class="post-toc-text">Pycharm快捷键</span></a></li></ol></li><li class="post-toc-item post-toc-level-3"><a class="post-toc-link" href="#字符串常见函数"><span class="post-toc-number">4.</span> <span class="post-toc-text">字符串常见函数</span></a><ol class="post-toc-child"><li class="post-toc-item post-toc-level-4"><a class="post-toc-link" href="#下标和切片"><span class="post-toc-number">4.1.</span> <span class="post-toc-text">下标和切片</span></a></li><li class="post-toc-item post-toc-level-4"><a class="post-toc-link" href="#字符串常见操作"><span class="post-toc-number">4.2.</span> <span class="post-toc-text">字符串常见操作</span></a><ol class="post-toc-child"><li class="post-toc-item post-toc-level-5"><a class="post-toc-link" href="#查找"><span class="post-toc-number">4.2.1.</span> <span class="post-toc-text">查找</span></a></li><li class="post-toc-item post-toc-level-5"><a class="post-toc-link" href="#判断"><span class="post-toc-number">4.2.2.</span> <span class="post-toc-text">判断</span></a></li><li class="post-toc-item post-toc-level-5"><a class="post-toc-link" href="#修改大小写"><span class="post-toc-number">4.2.3.</span> <span class="post-toc-text">修改大小写</span></a></li></ol></li></ol></li><li class="post-toc-item post-toc-level-3"><a class="post-toc-link" href="#字符串的编码"><span class="post-toc-number">5.</span> <span class="post-toc-text">字符串的编码</span></a><ol class="post-toc-child"><li class="post-toc-item post-toc-level-4"><a class="post-toc-link" href="#字符串的编码集"><span class="post-toc-number">5.1.</span> <span class="post-toc-text">字符串的编码集</span></a></li></ol></li><li class="post-toc-item post-toc-level-3"><a class="post-toc-link" href="#成员运算符"><span class="post-toc-number">6.</span> <span class="post-toc-text">成员运算符</span></a></li><li class="post-toc-item post-toc-level-3"><a class="post-toc-link" href="#字符串的format方法"><span class="post-toc-number">7.</span> <span class="post-toc-text">字符串的format方法</span></a><ol class="post-toc-child"><li class="post-toc-item post-toc-level-4"><a class="post-toc-link" href="#方案一：可以使用-占位符来表示格式化一个字符串"><span class="post-toc-number">7.1.</span> <span class="post-toc-text">方案一：可以使用 % 占位符来表示格式化一个字符串</span></a></li><li class="post-toc-item post-toc-level-4"><a class="post-toc-link" href="#方案二：替换字段"><span class="post-toc-number">7.2.</span> <span class="post-toc-text">方案二：替换字段</span></a></li></ol></li></ol></li><li class="post-toc-item post-toc-level-2"><a class="post-toc-link" href="#列表的基本使用"><span class="post-toc-number"></span> <span class="post-toc-text">列表的基本使用</span></a><ol class="post-toc-child"><li class="post-toc-item post-toc-level-3"><a class="post-toc-link" href="#使用-list-函数生成列表："><span class="post-toc-number">1.</span> <span class="post-toc-text">使用 list() 函数生成列表：</span></a></li><li class="post-toc-item post-toc-level-3"><a class="post-toc-link" href="#列表的操作，增删改查："><span class="post-toc-number">2.</span> <span class="post-toc-text">列表的操作，增删改查：</span></a><ol class="post-toc-child"><li class="post-toc-item post-toc-level-4"><a class="post-toc-link" href="#增，方法"><span class="post-toc-number">2.1.</span> <span class="post-toc-text">增，方法</span></a></li><li class="post-toc-item post-toc-level-4"><a class="post-toc-link" href="#删，方法"><span class="post-toc-number">2.2.</span> <span class="post-toc-text">删，方法</span></a></li><li class="post-toc-item post-toc-level-4"><a class="post-toc-link" href="#改，方法"><span class="post-toc-number">2.3.</span> <span class="post-toc-text">改，方法</span></a></li><li class="post-toc-item post-toc-level-4"><a class="post-toc-link" href="#查，方法"><span class="post-toc-number">2.4.</span> <span class="post-toc-text">查，方法</span></a></li></ol></li><li class="post-toc-item post-toc-level-3"><a class="post-toc-link" href="#列表的遍历"><span class="post-toc-number">3.</span> <span class="post-toc-text">列表的遍历</span></a></li><li class="post-toc-item post-toc-level-3"><a class="post-toc-link" href="#交换两个变量"><span class="post-toc-number">4.</span> <span class="post-toc-text">交换两个变量</span></a></li><li class="post-toc-item post-toc-level-3"><a class="post-toc-link" href="#冒泡排序"><span class="post-toc-number">5.</span> <span class="post-toc-text">冒泡排序</span></a></li><li class="post-toc-item post-toc-level-3"><a class="post-toc-link" href="#列表的排序和反转"><span class="post-toc-number">6.</span> <span class="post-toc-text">列表的排序和反转</span></a></li><li class="post-toc-item post-toc-level-3"><a class="post-toc-link" href="#可变类型与不可变类型"><span class="post-toc-number">7.</span> <span class="post-toc-text">可变类型与不可变类型</span></a><ol class="post-toc-child"><li class="post-toc-item post-toc-level-4"><a class="post-toc-link" href="#列表的复制"><span class="post-toc-number">7.1.</span> <span class="post-toc-text">列表的复制</span></a><ol class="post-toc-child"><li class="post-toc-item post-toc-level-5"><a class="post-toc-link" href="#重构flag"><span class="post-toc-number">7.1.1.</span> <span class="post-toc-text">重构flag:</span></a></li></ol></li></ol></li><li class="post-toc-item post-toc-level-3"><a class="post-toc-link" href="#求列表里的最大数"><span class="post-toc-number">8.</span> <span class="post-toc-text">求列表里的最大数</span></a></li><li class="post-toc-item post-toc-level-3"><a class="post-toc-link" href="#Python删除元素"><span class="post-toc-number">9.</span> <span class="post-toc-text">Python删除元素</span></a></li><li class="post-toc-item post-toc-level-3"><a class="post-toc-link" href="#列表的嵌套"><span class="post-toc-number">10.</span> <span class="post-toc-text">列表的嵌套</span></a></li><li class="post-toc-item post-toc-level-3"><a class="post-toc-link" href="#列表推导式的使用"><span class="post-toc-number">11.</span> <span class="post-toc-text">列表推导式的使用</span></a></li><li class="post-toc-item post-toc-level-3"><a class="post-toc-link" href="#浅拷贝与深拷贝"><span class="post-toc-number">12.</span> <span class="post-toc-text">浅拷贝与深拷贝</span></a></li><li class="post-toc-item post-toc-level-3"><a class="post-toc-link" href="#元组"><span class="post-toc-number">13.</span> <span class="post-toc-text">元组</span></a><ol class="post-toc-child"><li class="post-toc-item post-toc-level-4"><a class="post-toc-link" href="#列表与元组的相互转换"><span class="post-toc-number">13.1.</span> <span class="post-toc-text">列表与元组的相互转换</span></a></li></ol></li><li class="post-toc-item post-toc-level-3"><a class="post-toc-link" href="#join-函数"><span class="post-toc-number">14.</span> <span class="post-toc-text">join()函数</span></a></li><li class="post-toc-item post-toc-level-3"><a class="post-toc-link" href="#字典的使用"><span class="post-toc-number">15.</span> <span class="post-toc-text">字典的使用</span></a><ol class="post-toc-child"><li class="post-toc-item post-toc-level-4"><a class="post-toc-link" href="#字典的增删改查"><span class="post-toc-number">15.1.</span> <span class="post-toc-text">字典的增删改查</span></a></li><li class="post-toc-item post-toc-level-4"><a class="post-toc-link" href="#update方法的使用"><span class="post-toc-number">15.2.</span> <span class="post-toc-text">update方法的使用</span></a></li></ol></li><li class="post-toc-item post-toc-level-3"><a class="post-toc-link" href="#字典的遍历"><span class="post-toc-number">16.</span> <span class="post-toc-text">字典的遍历</span></a><ol class="post-toc-child"><li class="post-toc-item post-toc-level-4"><a class="post-toc-link" href="#字典推导式之键值颠倒"><span class="post-toc-number">16.1.</span> <span class="post-toc-text">字典推导式之键值颠倒</span></a></li><li class="post-toc-item post-toc-level-4"><a class="post-toc-link" href="#案例之作业讲解"><span class="post-toc-number">16.2.</span> <span class="post-toc-text">案例之作业讲解</span></a></li></ol></li><li class="post-toc-item post-toc-level-3"><a class="post-toc-link" href="#集合的基本使用"><span class="post-toc-number">17.</span> <span class="post-toc-text">集合的基本使用</span></a><ol class="post-toc-child"><li class="post-toc-item post-toc-level-4"><a class="post-toc-link" href="#集合运算符的使用"><span class="post-toc-number">17.1.</span> <span class="post-toc-text">集合运算符的使用</span></a></li><li class="post-toc-item post-toc-level-4"><a class="post-toc-link" href="#集合的练习，去重排序"><span class="post-toc-number">17.2.</span> <span class="post-toc-text">集合的练习，去重排序</span></a></li><li class="post-toc-item post-toc-level-4"><a class="post-toc-link" href="#使用内置类进行数据转化"><span class="post-toc-number">17.3.</span> <span class="post-toc-text">使用内置类进行数据转化</span></a></li><li class="post-toc-item post-toc-level-4"><a class="post-toc-link" href="#eval-直接执行语法字符串的内容"><span class="post-toc-number">17.4.</span> <span class="post-toc-text">eval()直接执行语法字符串的内容</span></a></li><li class="post-toc-item post-toc-level-4"><a class="post-toc-link" href="#json-将其他数据类型转换成json字符串"><span class="post-toc-number">17.5.</span> <span class="post-toc-text">json() 将其他数据类型转换成json字符串</span></a></li></ol></li><li class="post-toc-item post-toc-level-3"><a class="post-toc-link" href="#公共方法"><span class="post-toc-number">18.</span> <span class="post-toc-text">公共方法</span></a></li><li class="post-toc-item post-toc-level-3"><a class="post-toc-link" href="#函数"><span class="post-toc-number">19.</span> <span class="post-toc-text">函数</span></a><ol class="post-toc-child"><li class="post-toc-item post-toc-level-4"><a class="post-toc-link" href="#传参的方式"><span class="post-toc-number">19.1.</span> <span class="post-toc-text">传参的方式</span></a></li><li class="post-toc-item post-toc-level-4"><a class="post-toc-link" href="#返回值"><span class="post-toc-number">19.2.</span> <span class="post-toc-text">返回值</span></a></li><li class="post-toc-item post-toc-level-4"><a class="post-toc-link" href="#函数的注释"><span class="post-toc-number">19.3.</span> <span class="post-toc-text">函数的注释</span></a></li><li class="post-toc-item post-toc-level-4"><a class="post-toc-link" href="#函数的调用"><span class="post-toc-number">19.4.</span> <span class="post-toc-text">函数的调用</span></a></li><li class="post-toc-item post-toc-level-4"><a class="post-toc-link" href="#全局变量和局部变量"><span class="post-toc-number">19.5.</span> <span class="post-toc-text">全局变量和局部变量</span></a><ol class="post-toc-child"><li class="post-toc-item post-toc-level-5"><a class="post-toc-link" href="#内置函数globals（）-和-locals（）"><span class="post-toc-number">19.5.1.</span> <span class="post-toc-text">内置函数globals（） 和 locals（）</span></a></li><li class="post-toc-item post-toc-level-5"><a class="post-toc-link" href="#多值返回"><span class="post-toc-number">19.5.2.</span> <span class="post-toc-text">多值返回</span></a></li></ol></li></ol></li><li class="post-toc-item post-toc-level-3"><a class="post-toc-link" href="#函数回顾"><span class="post-toc-number">20.</span> <span class="post-toc-text">函数回顾</span></a></li><li class="post-toc-item post-toc-level-3"><a class="post-toc-link" href="#函数的缺省参数"><span class="post-toc-number">21.</span> <span class="post-toc-text">函数的缺省参数</span></a></li><li class="post-toc-item post-toc-level-3"><a class="post-toc-link" href="#可变参数"><span class="post-toc-number">22.</span> <span class="post-toc-text">可变参数</span></a></li><li class="post-toc-item post-toc-level-3"><a class="post-toc-link" href="#可变类型和不可变数据类型"><span class="post-toc-number">23.</span> <span class="post-toc-text">可变类型和不可变数据类型</span></a><ol class="post-toc-child"><li class="post-toc-item post-toc-level-4"><a class="post-toc-link" href="#函数的注意事项"><span class="post-toc-number">23.1.</span> <span class="post-toc-text">函数的注意事项</span></a></li><li class="post-toc-item post-toc-level-4"><a class="post-toc-link" href="#递归函数的使用"><span class="post-toc-number">23.2.</span> <span class="post-toc-text">递归函数的使用</span></a></li><li class="post-toc-item post-toc-level-4"><a class="post-toc-link" href="#匿名函数"><span class="post-toc-number">23.3.</span> <span class="post-toc-text">匿名函数</span></a></li></ol></li><li class="post-toc-item post-toc-level-3"><a class="post-toc-link" href="#列表相关的一些方法"><span class="post-toc-number">24.</span> <span class="post-toc-text">列表相关的一些方法</span></a></li><li class="post-toc-item post-toc-level-3"><a class="post-toc-link" href="#内置函数的总结"><span class="post-toc-number">25.</span> <span class="post-toc-text">内置函数的总结</span></a></li><li class="post-toc-item post-toc-level-3"><a class="post-toc-link" href="#高阶函数"><span class="post-toc-number">26.</span> <span class="post-toc-text">高阶函数</span></a><ol class="post-toc-child"><li class="post-toc-item post-toc-level-4"><a class="post-toc-link" href="#闭包的概念，应该归到高阶函数3"><span class="post-toc-number">26.1.</span> <span class="post-toc-text">闭包的概念，应该归到高阶函数3</span></a><ol class="post-toc-child"><li class="post-toc-item post-toc-level-5"><a class="post-toc-link" href="#闭包条件："><span class="post-toc-number">26.1.1.</span> <span class="post-toc-text">闭包条件：</span></a></li><li class="post-toc-item post-toc-level-5"><a class="post-toc-link" href="#时间戳"><span class="post-toc-number">26.1.2.</span> <span class="post-toc-text">时间戳</span></a></li></ol></li></ol></li><li class="post-toc-item post-toc-level-3"><a class="post-toc-link" href="#装饰器的使用"><span class="post-toc-number">27.</span> <span class="post-toc-text">装饰器的使用</span></a></li></ol>
        </nav>
    </aside>


<article id="post-千峰教程笔记"
  class="post-article article-type-post fade" itemprop="blogPost">

    <div class="post-card">
        <h1 class="post-card-title">千峰教程笔记</h1>
        <div class="post-meta">
            <time class="post-time" title="2020-07-16 15:20:36" datetime="2020-07-16T07:20:36.000Z"  itemprop="datePublished">2020-07-16</time>

            
	<ul class="article-category-list"><li class="article-category-list-item"><a class="article-category-list-link" href="/blog/categories/%E7%BC%96%E7%A8%8B/">编程</a></li></ul>



            
<span id="busuanzi_container_page_pv" title="文章总阅读量" style='display:none'>
    <i class="icon icon-eye icon-pr"></i><span id="busuanzi_value_page_pv"></span>
</span>


        </div>
        <div class="post-content" id="post-content" itemprop="postContent">
            <p>input获得的数据都是字符串型，所以要补充一些类型转换函数</p>
<h3 id="布尔型"><a href="#布尔型" class="headerlink" title="布尔型"></a>布尔型</h3><p>数字非0即是True</p>
<p>字符串非None</p>
<h3 id="列表类型"><a href="#列表类型" class="headerlink" title="#列表类型"></a>#列表类型</h3><p>names =[‘姚万万’， ‘邹碧慧’，’郑美水’，’蔡顺利’，’李想’， ‘蔡坤’]</p>
<p>空列表是 [ ]</p>
<h3 id="字典类型"><a href="#字典类型" class="headerlink" title="#字典类型"></a>#字典类型</h3><p>person = {‘name’: ‘ 袁奋’，’age’: 18，’addr’: ， 湖北’，’ 身高’: ‘180cm’}</p>
<p>空字典是 {}</p>
<h3 id="元组类型"><a href="#元组类型" class="headerlink" title="#元组类型"></a>#元组类型</h3><p>nums=(1,8,9,2,3,0) 空元组是()</p>
<h3 id="集合类型"><a href="#集合类型" class="headerlink" title="#集合类型"></a>#集合类型</h3><p>X={9，hello’ ,’hi’,’good’}</p>
<p>空集合是set()</p>
<h2 id="类型的查看"><a href="#类型的查看" class="headerlink" title="类型的查看"></a>类型的查看</h2><p>使用内置函数type()</p>
<h2 id="不同进制数的表示以及转换"><a href="#不同进制数的表示以及转换" class="headerlink" title="不同进制数的表示以及转换"></a>不同进制数的表示以及转换</h2><ul>
<li>二进制     0b…  可使用bin()内置函数转换后输出</li>
<li>八进制     0o…  可使用oct()内置函数转换后输出</li>
<li>十六进制  0x…  可使用hex()内置函数转换后输出</li>
<li>能不能通过此方法获取暂未尝试</li>
</ul>
<h2 id="类型转换函数-应该是称为内置类"><a href="#类型转换函数-应该是称为内置类" class="headerlink" title="类型转换函数(应该是称为内置类)"></a>类型转换函数(应该是称为内置类)</h2><p>str() 将变量转换为字符串</p>
<p>int() 将变量转换为整型,不改变原变量数据类型 是int(value)参与运算</p>
<p>含有多个参数时,写作int(valuename,进制数),表示这个原数据是多少进制的,但转换结果为十进制</p>
<p>float()转换为浮点数类型</p>
<h2 id="将数据转换为布尔型"><a href="#将数据转换为布尔型" class="headerlink" title="将数据转换为布尔型"></a>将数据转换为布尔型</h2><ul>
<li><p><em>使用bool()内置类来进行数据转换</em></p>
</li>
<li><p>啥都不写也是个False</p>
</li>
<li><p>数字转换成bool型,负数也行 非0即是True</p>
</li>
<li><p>字符串转换为bool值,易错点 bool(‘False’)=&gt;True 字</p>
<ul>
<li>符串里只有空单双引号和None为False,None称为空数据</li>
<li>None加了引号就是True了，陷阱颇多</li>
<li>Python里面没有Null这个关键字</li>
</ul>
</li>
<li><p>空列表[ ] False</p>
</li>
<li><p>空元组( ) False</p>
</li>
<li><p>空字典{ } False</p>
<ul>
<li>字典{‘key’:’value’,…} </li>
<li>集合{value,…}</li>
<li>集合与字典区别: 数据是否成对出现</li>
</ul>
</li>
<li><p>空集合s=set()    print(bool(s))  =&gt;  False</p>
</li>
</ul>
<h2 id="比较运算"><a href="#比较运算" class="headerlink" title="比较运算"></a>比较运算</h2><p>字符串进行比较运算时，只比较不相同的字母的<em>ASCII码值</em></p>
<h2 id="算术运算符"><a href="#算术运算符" class="headerlink" title="算术运算符"></a>算术运算符</h2><ul>
<li>加减乘除分别写作 + - * /</li>
<li>这个除法运算得到的是浮点数，要想整除使用 //</li>
<li>取(模)余运算 %</li>
<li>乘方,开方 **</li>
<li>遵从数学运算的优先级,有括号先算括号里边的</li>
</ul>
<h5 id="字符串里有限度的支持加法和乘法运算"><a href="#字符串里有限度的支持加法和乘法运算" class="headerlink" title="字符串里有限度的支持加法和乘法运算"></a>字符串里有限度的支持加法和乘法运算</h5><ul>
<li><p>加法 拼接多个字符串,需要注意拼接数字时需要进行类型转换,没有间隔符</p>
</li>
<li><p>乘法 重复输出,没有间隔符</p>
</li>
<li><p>Python不支持字符串与数字类型进行加法运算,减法等其他运算都不支持</p>
</li>
</ul>
<h2 id="赋值运算符"><a href="#赋值运算符" class="headerlink" title="赋值运算符"></a>赋值运算符</h2><p>右向左运算,左边是变量</p>
<p>复合运算符,理解即可</p>
<p>或许利用input结合可以直接获得运算数据,可以实现</p>
<p>可以实现取模等于符合运算  幂等于复合运算  取模等于复合运算</p>
<p>算术运算符里的整除,-5//2=-3,用向下取整,取出来还真是int型数据,来理解</p>
<h3 id="传递赋值-连续赋值"><a href="#传递赋值-连续赋值" class="headerlink" title="传递赋值(连续赋值)"></a>传递赋值(连续赋值)</h3><p>a=b=c=d=”hello” 可以</p>
<p>a=’yes’=b=”no” 错误(yes是常量)</p>
<p>m,n=3,5 可以，省略括号的元组，拆包，元组章杰在细讲</p>
<p>x=’hello’,’good’,’yes’  输出为带圆括号的元组</p>
<p>两边变量与值得数量不相等，拆包出错</p>
<p>o,p,q=1,2,3,4,5,6 （不存在等量分配这一说！切记这是个易错点）</p>
<p>可以用*valuename来实现长度可变的赋值，打包剩余所有变量，然后这个拥有多个值的变量就成为了列表类型</p>
<h2 id="比较运算符"><a href="#比较运算符" class="headerlink" title="比较运算符"></a>比较运算符</h2><p>巴拉巴拉简单</p>
<h2 id="逻辑运算符"><a href="#逻辑运算符" class="headerlink" title="逻辑运算符"></a>逻辑运算符</h2><p>注 意短路现象</p>
<p>后面竟然可以接语句，这是我没想到到，</p>
<p>逻辑运算的结果可能是0,1，不一定是布尔值</p>
<h2 id="位运算符"><a href="#位运算符" class="headerlink" title="位运算符"></a>位运算符</h2><p>转为二进制，按位运算，二进制运算完后在转换为10进制数</p>
<p>按位与 &amp; </p>
<p>​    二进制竖着看，左往右，同为1，异为0</p>
<p>按位或 |</p>
<p>​    只要有一个为1则为1</p>
<p>按位异或 ^</p>
<p>​    同为0，异为1    比较完后，转为10进制并返回结果</p>
<p>按位左移 &lt;&lt;</p>
<p>​    转为二进制，向左移，右补零。n位即：原数<em>2*</em>n</p>
<p>按位右移 &gt;&gt;</p>
<p>​    转为二进制，向右移，左补零。n位即：原数/(2**n) 结果类型为原数类型</p>
<h2 id="逻辑运算符的优先级"><a href="#逻辑运算符的优先级" class="headerlink" title="逻辑运算符的优先级"></a>逻辑运算符的优先级</h2><p>not&gt;and&gt;or</p>
<p>另外要注意短路现象和括号</p>
<h2 id="分支语句"><a href="#分支语句" class="headerlink" title="分支语句"></a>分支语句</h2><p><em>不支持switch…case…语句!</em>，强制缩进表示语句层级</p>
<p>if </p>
<p>if…else</p>
<p>随机数random(m,n)模块，使用random.randint(m,n)类，生成随机数</p>
<p>if…elif…elif…else…</p>
<p>下面写一个剪刀石头布的案例，因优化了交互，代码较多，但内容简单</p>
<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment"># 会用到随机数模块random()</span></span><br><span class="line"><span class="keyword">import</span> random</span><br><span class="line"></span><br><span class="line"><span class="comment"># 获取用户猜拳选项</span></span><br><span class="line">player = input(<span class="string">"请输入你的猜拳选项，直接输入中文即可"</span>)</span><br><span class="line"><span class="comment"># print(player)</span></span><br><span class="line"><span class="keyword">if</span> player == <span class="string">'剪刀'</span>:</span><br><span class="line">    player = <span class="number">0</span></span><br><span class="line"><span class="keyword">if</span> player == <span class="string">'石头'</span>:</span><br><span class="line">    player = <span class="number">1</span></span><br><span class="line"><span class="keyword">if</span> player == <span class="string">'布'</span>:</span><br><span class="line">    player = <span class="number">2</span></span><br><span class="line"><span class="comment"># 生成机器猜拳选项</span></span><br><span class="line">computer = random.randint(<span class="number">0</span>, <span class="number">2</span>)</span><br><span class="line"><span class="comment"># 判断模块</span></span><br><span class="line"><span class="keyword">if</span> player == <span class="number">0</span> <span class="keyword">and</span> computer == <span class="number">2</span>:</span><br><span class="line">    print(<span class="string">'机器猜的是布'</span>)</span><br><span class="line">    print(<span class="string">'所以，本局你赢了'</span>)</span><br><span class="line"><span class="keyword">elif</span> player == <span class="number">1</span> <span class="keyword">and</span> computer == <span class="number">0</span>:</span><br><span class="line">    print(<span class="string">'机器猜的是剪刀'</span>)</span><br><span class="line">    print(<span class="string">'所以，本局你赢了'</span>)</span><br><span class="line"><span class="keyword">elif</span> player == <span class="number">2</span> <span class="keyword">and</span> computer == <span class="number">1</span>:</span><br><span class="line">    print(<span class="string">'机器猜的是石头'</span>)</span><br><span class="line">    print(<span class="string">'所以，本局你赢了'</span>)</span><br><span class="line"><span class="keyword">elif</span> player == computer:</span><br><span class="line">    print(<span class="string">"无论如何，菜批你输了！"</span>)</span><br><span class="line"><span class="keyword">else</span>:</span><br><span class="line">    print(<span class="string">'两者相同，所以本局平局'</span>)</span><br><span class="line"><span class="comment"># 学到了这个elif最终只能跟一个else，并不能每一步的elif都跟一个</span></span><br></pre></td></tr></table></figure>

<p>数据的类型转换可以在判断前,也可以在判断中</p>
<p>​    在判断中if语句以及else语句后边都要有冒号,我老是忘记冒号???。</p>
<p>pass关键字 单纯保证结构的完整性，使程序不报错</p>
<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment"># 用来单纯占位，保证语句的完整性</span></span><br><span class="line">age = <span class="number">18</span></span><br><span class="line"><span class="keyword">if</span> age &gt; <span class="number">18</span> :</span><br><span class="line">    <span class="keyword">pass</span></span><br></pre></td></tr></table></figure>

<h4 id="小结："><a href="#小结：" class="headerlink" title="小结："></a>小结：</h4><p>区间范围不能连写</p>
<p>if条件需要的是bool类型的值，如果不是会自动转换（隐式类型转换）</p>
<p>三元表达式，对if。。。else。。。的简写</p>
<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br></pre></td><td class="code"><pre><span class="line">num1 = int(input(<span class="string">'输入数字1'</span>))</span><br><span class="line">num2 = int(input(<span class="string">'输入数字2'</span>))</span><br><span class="line"><span class="keyword">if</span> num1&gt;num2:</span><br><span class="line">    x=num1</span><br><span class="line"><span class="keyword">else</span>:</span><br><span class="line">    x=num2</span><br><span class="line">print(x)</span><br><span class="line"><span class="comment"># 使用三元表达式简写</span></span><br><span class="line">x = num1 <span class="keyword">if</span> num1 &gt; num2 <span class="keyword">else</span> num2</span><br><span class="line">print(x)</span><br></pre></td></tr></table></figure>

<h2 id="循环语句"><a href="#循环语句" class="headerlink" title="循环语句"></a>循环语句</h2><p>Python里边不支持do…while循环</p>
<p>Python里面没有自增自减运算符</p>
<h4 id="while循环"><a href="#while循环" class="headerlink" title="while循环"></a>while循环</h4><figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment"># 这个x需要提前定义,for循环里不用</span></span><br><span class="line">x = <span class="number">0</span></span><br><span class="line"><span class="keyword">while</span> x &lt; <span class="number">5</span>:</span><br><span class="line">    print(<span class="string">'Hello World'</span>, int(x))</span><br><span class="line">    x += <span class="number">1</span></span><br><span class="line">print(x)  <span class="comment"># 最终值是5</span></span><br></pre></td></tr></table></figure>

<h4 id="for循环"><a href="#for循环" class="headerlink" title="for循环"></a>for循环</h4><p>指的就是for…in…循环，我选择直接上乘法表</p>
<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment"># 规定中去尾，我们需要设置到10</span></span><br><span class="line"><span class="comment"># 在没详细了解Python的输出函数之前，也可以实现简单的使用</span></span><br><span class="line"><span class="keyword">for</span> j <span class="keyword">in</span> range(<span class="number">1</span>, <span class="number">10</span>):</span><br><span class="line">    <span class="keyword">for</span> i <span class="keyword">in</span> range(<span class="number">1</span>, j + <span class="number">1</span>):</span><br><span class="line">        print(str(i) + <span class="string">"*"</span> + str(j) + <span class="string">'='</span> + str(i * j), end=<span class="string">' '</span>)</span><br><span class="line">    print()</span><br></pre></td></tr></table></figure>

<p>in后面必须是一个可迭代对象</p>
<h4 id="break与continue"><a href="#break与continue" class="headerlink" title="break与continue"></a>break与continue</h4><p>直接照搬C语言里的理解，<u>break结束本层循环，continue结束本次循环</u></p>
<p>高级案例，打印素数</p>
<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment"># 打印并输出101到200内的素数，竟然有for...else...语句</span></span><br><span class="line"><span class="keyword">for</span> i <span class="keyword">in</span> range(<span class="number">101</span>, <span class="number">200</span>):</span><br><span class="line">    <span class="keyword">for</span> j <span class="keyword">in</span> range(<span class="number">2</span>, int(i ** <span class="number">0.5</span>)+<span class="number">1</span>):</span><br><span class="line">        <span class="keyword">if</span> i % j == <span class="number">0</span>:</span><br><span class="line">            <span class="keyword">break</span></span><br><span class="line">    <span class="keyword">else</span>:</span><br><span class="line">        <span class="comment"># 当循环里的break没有被执行时，会执行else语句</span></span><br><span class="line">        print(i)</span><br></pre></td></tr></table></figure>

<h2 id="字符串"><a href="#字符串" class="headerlink" title="字符串"></a>字符串</h2><h3 id="字符串的表示方式"><a href="#字符串的表示方式" class="headerlink" title="字符串的表示方式"></a>字符串的表示方式</h3><p>使用一对单引号，一对双引号，<strong>三对单引号，三对双引号包裹</strong></p>
<p>亦可以嵌套使用</p>
<p>转义字符 \ </p>
<h3 id="字符串的下标和切片"><a href="#字符串的下标和切片" class="headerlink" title="字符串的下标和切片"></a>字符串的下标和切片</h3><h4 id="下标又称为索引"><a href="#下标又称为索引" class="headerlink" title="下标又称为索引"></a>下标又称为索引</h4><p>字符串是不可变的数据类型，replace也不行</p>
<p>输出方式为，一个串</p>
<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line">str = <span class="string">"bugsong"</span></span><br><span class="line">print(str)</span><br><span class="line">print(type(str))</span><br><span class="line"><span class="comment"># bugsong &lt;class 'str'&gt;</span></span><br></pre></td></tr></table></figure>

<h4 id="切片为赋值一部分内容生成一个新的内容"><a href="#切片为赋值一部分内容生成一个新的内容" class="headerlink" title="切片为赋值一部分内容生成一个新的内容"></a>切片为赋值一部分内容生成一个新的内容</h4><p>切片的语法 valuename[start：end:step] 奈何我typora用得不好</p>
<p>step，步长，<u>可以理解为间隔</u></p>
<p>有头无尾，正序从0开始                                                                                                                                        </p>
<p>负数作为尾部的话，从-1开始</p>
<h3 id="字符串常见的操作"><a href="#字符串常见的操作" class="headerlink" title="字符串常见的操作"></a>字符串常见的操作</h3><p>获取长度 len() 函数</p>
<h4 id="查找内容相关方法："><a href="#查找内容相关方法：" class="headerlink" title="查找内容相关方法："></a>查找内容相关方法：</h4><ul>
<li>​    find() valuename.find()，获得下标，从零开始查，如果字符不存在于字符串，<u>结果为-1</u></li>
<li>​    rfind()</li>
<li>​    index() valuename.index()，获得下标，如果字符不存在于字符串，<u>会报错</u></li>
<li>​    rindex()</li>
</ul>
<h4 id="判断相关方法："><a href="#判断相关方法：" class="headerlink" title="判断相关方法："></a>判断相关方法：</h4><p>is开头，结果是bool值，</p>
<ul>
<li>startswith() 以什么开头，可以是字符(串)</li>
<li>endswith() 以什么结尾</li>
<li>isdigit() 是否是数字，还不能是小数</li>
<li>isalpha() 是否单纯包含字符，数字</li>
</ul>
<h4 id="replace-方法"><a href="#replace-方法" class="headerlink" title="replace()方法"></a>replace()方法</h4><figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment"># 字符串相关方法</span></span><br><span class="line">str = <span class="string">'sdajfklsjadfkljskldf'</span></span><br><span class="line">print(len(str))</span><br><span class="line">words = str.replace(<span class="string">'s'</span>, <span class="string">'-'</span>)</span><br><span class="line">print(str)</span><br><span class="line">print(words)</span><br><span class="line"><span class="comment"># 再次印证字符串是不可变数据类型</span></span><br><span class="line"><span class="comment"># 20</span></span><br><span class="line"><span class="comment"># sdajfklsjadfkljskldf</span></span><br><span class="line"><span class="comment"># -dajfkl-jadfklj-kldf</span></span><br></pre></td></tr></table></figure>

<p>全部替换，生成副本</p>
<h4 id="内容分隔相关操作-带r的与不带r的好像就是从哪边开始的意思"><a href="#内容分隔相关操作-带r的与不带r的好像就是从哪边开始的意思" class="headerlink" title="内容分隔相关操作(带r的与不带r的好像就是从哪边开始的意思)"></a>内容分隔相关操作(带r的与不带r的好像就是从哪边开始的意思)</h4><ul>
<li>split() 分隔，用于将字符串分割为列表，valuename.split(‘分隔符’)，分隔符会在结果中消失，生成标准的列表。可以给定逗号后参数，限制分隔数，则后面一堆为一个列表元素，正序切入</li>
<li>rsiplit() 生成结果没有区别，可以给定逗号后参数，限制分隔数，则后面一堆为一个列表元素，逆序切入</li>
<li>splitlines() 换行 \n  分隔</li>
<li>partition()  制定一个字符串作为分隔符，即使有两个指定符号分为三部分，前面，分隔符本身，后面，结果是一个元组</li>
<li>rpartition() 从右往左的分隔，barabara…</li>
</ul>
<h4 id="Pycharm使用建议"><a href="#Pycharm使用建议" class="headerlink" title="Pycharm使用建议"></a>Pycharm使用建议</h4><p><u>Pycharm内的Python文档查询，按住Ctrl键点击要查询的名字，来到对应文档，在项目窗口的右上角单击瞄准镜，即可来到包含文件，右上角齿轮可以设置显示更细致的内容，请选择Show Members</u></p>
<h4 id="Pycharm快捷键"><a href="#Pycharm快捷键" class="headerlink" title="Pycharm快捷键"></a>Pycharm快捷键</h4><p>双击 shift ：全局搜索功能，jetbrain软件的通用快捷键</p>
<p>​    关闭方法：双击shift打开全局搜索–&gt;Actions–&gt;registry–&gt;ide.suppress.double.click.hander–&gt;勾选即可</p>
<p>​    回调：Ctrl Shift A</p>
<p>格式化代码：Ctrl Alt L</p>
<p>复制选中代码：Ctrl D</p>
<p>移动选中代码：Ctrl Shift + ↑/↓ emmmm好像Alt也行</p>
<p>Ctrl Enter：向当前行的下行增生一行，另外向上增生貌似没作用？</p>
<p>更多快捷键详见：Settings–&gt;Keymap–&gt;barabara…</p>
<h3 id="字符串常见函数"><a href="#字符串常见函数" class="headerlink" title="字符串常见函数"></a>字符串常见函数</h3><h4 id="下标和切片"><a href="#下标和切片" class="headerlink" title="下标和切片"></a>下标和切片</h4><p>words = ‘abcdefghijklmnopqrstuvwxyz’</p>
<p>words[2] 根据下表获取元素</p>
<p>words[start：end：step]</p>
<h4 id="字符串常见操作"><a href="#字符串常见操作" class="headerlink" title="字符串常见操作"></a>字符串常见操作</h4><p>获取长度 len()</p>
<h5 id="查找"><a href="#查找" class="headerlink" title="查找"></a>查找</h5><p>index() 方法  rindex() 方法</p>
<p>find() 方法 rfind() 方法</p>
<h5 id="判断"><a href="#判断" class="headerlink" title="判断"></a>判断</h5><p>isalpha() 方法</p>
<p>isdigist() 方法</p>
<p>isspace() 方法</p>
<p>startswith() 方法</p>
<p>endswith() 方法</p>
<p>另外务必学会如何查看Pycharm自带文档</p>
<h5 id="修改大小写"><a href="#修改大小写" class="headerlink" title="修改大小写"></a>修改大小写</h5><p>capitalize() <u>仅大写第一个单词的首字母</u>，应该用用于处理英文句子</p>
<p>title() 方法 单词首字母大写</p>
<p>upper() 方法 全大写</p>
<p>lower() 方法 全小写</p>
<p>ljust() 方法 让字符串以指定长度显示，少则补全空格，多则不作操作。可根据参数自行研究</p>
<p>rjust() 方法</p>
<p>center() 方法 单词居中，两边加字符</p>
<p>strip() 方法 清除指定字符，默认是空白符 rstrip()  lstrip()</p>
<p>join() 方法 指定符号分隔元素</p>
<h3 id="字符串的编码"><a href="#字符串的编码" class="headerlink" title="字符串的编码"></a>字符串的编码</h3><p>理解二进制，ASCII 码表</p>
<p>二进制最高位表示正负</p>
<p>ASCII Latin1 Unicode编码</p>
<h4 id="字符串的编码集"><a href="#字符串的编码集" class="headerlink" title="字符串的编码集"></a>字符串的编码集</h4><p>使用内置函数 chr() 和 ord()能够查看数字和字符的对应关系</p>
<p>ord(‘待查询的字符’) 可获得对应编码(基于Unicode)</p>
<p>chr(待查询的编码序号)</p>
<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line">s1=<span class="string">'你'</span>.encode(<span class="string">'utf-8'</span>)  <span class="comment"># 使用utf-8的格式编码</span></span><br><span class="line">print(type(s1))  <span class="comment"># &lt;class 'bytes'&gt;字节类型</span></span><br><span class="line">print(s1)  <span class="comment"># b'\xe4\xbd\xa0' 转换为对应二进制为</span></span><br><span class="line">s2= s1.decode(<span class="string">'utf-8'</span>)  <span class="comment"># 使用utf-8的格式解码</span></span><br><span class="line">print(s2)  <span class="comment"># 你</span></span><br></pre></td></tr></table></figure>

<h3 id="成员运算符"><a href="#成员运算符" class="headerlink" title="成员运算符"></a>成员运算符</h3><p><u>语法规则：要判断的元素 in 可迭代对象</u></p>
<p>in barabara</p>
<p>not in barabara</p>
<p>此处可能应用到 for-else 语句</p>
<h3 id="字符串的format方法"><a href="#字符串的format方法" class="headerlink" title="字符串的format方法"></a>字符串的format方法</h3><h4 id="方案一：可以使用-占位符来表示格式化一个字符串"><a href="#方案一：可以使用-占位符来表示格式化一个字符串" class="headerlink" title="方案一：可以使用 % 占位符来表示格式化一个字符串"></a>方案一：可以使用 % 占位符来表示格式化一个字符串</h4><figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line">name = <span class="string">'Wiy Song'</span></span><br><span class="line">age = <span class="number">18</span></span><br><span class="line">print(<span class="string">'大家好，我是%s，今年%d岁了'</span> % (name, age))</span><br><span class="line"><span class="comment"># 大家好，我是Wiy Song，今年18岁了</span></span><br></pre></td></tr></table></figure>

<p>多参数表用圆括号包裹，前有%，再往前<em>没有逗号</em></p>
<p>详参百度</p>
<h4 id="方案二：替换字段"><a href="#方案二：替换字段" class="headerlink" title="方案二：替换字段"></a>方案二：替换字段</h4><p>语法实例：（x,y两个案例不建议混用）</p>
<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br></pre></td><td class="code"><pre><span class="line">x = <span class="string">'大家好，我是&#123;&#125;，今年&#123;&#125;岁了'</span>.format(<span class="string">'Wiy Song'</span>, <span class="number">18</span>)</span><br><span class="line">print(x)  <span class="comment"># 大家好，我是Wiy Song，今年18岁了</span></span><br><span class="line"><span class="comment"># &#123;&#125;内什么都不写，会一一对应来填充,不和y案例混用</span></span><br><span class="line">y = <span class="string">'大家好，我是&#123;1&#125;，今年&#123;0&#125;岁了'</span>.format(<span class="number">18</span>, <span class="string">'Wiy Song'</span>)</span><br><span class="line">print(y)  <span class="comment"># 大家好，我是Wiy Song，今年18岁了</span></span><br><span class="line"><span class="comment"># &#123;数字&#125;内写的是索引，从后面的参数表中来寻找</span></span><br><span class="line">z = <span class="string">'大家好，我是&#123;name&#125;，今年&#123;age&#125;岁了'</span>.format(age=<span class="number">18</span>, name=<span class="string">'Wiy Song'</span>)</span><br><span class="line">print(z)  <span class="comment"># 大家好，我是Wiy Song，今年18岁了</span></span><br><span class="line"><span class="comment"># &#123;valuename&#125;内写变量名，从后面的参数表中来寻找</span></span><br><span class="line"><span class="comment"># 混合使用时，键-值对的方式写在最后边</span></span><br></pre></td></tr></table></figure>

<h2 id="列表的基本使用"><a href="#列表的基本使用" class="headerlink" title="列表的基本使用"></a>列表的基本使用</h2><h3 id="使用-list-函数生成列表："><a href="#使用-list-函数生成列表：" class="headerlink" title="使用 list() 函数生成列表："></a>使用 list() 函数生成列表：</h3><figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment"># 使用list() 将可迭代对象转换成为一个列表</span></span><br><span class="line">names = list((<span class="string">'wanisong'</span>, <span class="string">'nareii'</span>))</span><br><span class="line">print(names)</span><br><span class="line"><span class="comment"># list() 的参数是可迭代对象，我们将名字设置成了元组，包含为list() 的参数</span></span><br><span class="line"><span class="comment"># ['wanisong', 'nareii']</span></span><br><span class="line"></span><br><span class="line"><span class="comment"># 字符串不能通过下表修改，但是列表可以</span></span><br><span class="line"><span class="comment"># 通过索引来修改,从零开始</span></span><br><span class="line">names[<span class="number">1</span>] = <span class="string">'saka'</span></span><br><span class="line">print(names)</span><br><span class="line"><span class="comment"># ['wanisong', 'saka']</span></span><br></pre></td></tr></table></figure>

<h3 id="列表的操作，增删改查："><a href="#列表的操作，增删改查：" class="headerlink" title="列表的操作，增删改查："></a>列表的操作，增删改查：</h3><h4 id="增，方法"><a href="#增，方法" class="headerlink" title="增，方法"></a>增，方法</h4><ul>
<li>append() 在列表最后面追加，永久性</li>
<li>insert() 参数index表示下标，object表示数据，占据原来的位置，其余位置后移一格</li>
<li>extend() 参数是可迭代对象，追加到末尾，也可以合并两个列表</li>
</ul>
<h4 id="删，方法"><a href="#删，方法" class="headerlink" title="删，方法"></a>删，方法</h4><ul>
<li>pop() 弹出最后一个元素，也可以提供index来删除指定元素，object可被继承与新的变量</li>
<li>remove() 删除指定object，若元素不存在，报错</li>
<li>clear() 清空一个列表</li>
</ul>
<p>del 删除语句，狠一点，直接除根</p>
<h4 id="改，方法"><a href="#改，方法" class="headerlink" title="改，方法"></a>改，方法</h4><p>直接通过下标修改</p>
<h4 id="查，方法"><a href="#查，方法" class="headerlink" title="查，方法"></a>查，方法</h4><ul>
<li>index() 提供object，返回index！，若不存在则报错</li>
<li>count() 提供object，返回在列表中的出现次数</li>
<li>in 成员运算符</li>
</ul>
<h3 id="列表的遍历"><a href="#列表的遍历" class="headerlink" title="列表的遍历"></a>列表的遍历</h3><p>for-in 循环，不断调用迭代器的 next() 方法</p>
<h3 id="交换两个变量"><a href="#交换两个变量" class="headerlink" title="交换两个变量"></a>交换两个变量</h3><p>使用异或运算符,高级</p>
<p>a = a ^ b</p>
<p>b = a ^ b</p>
<p>a = a ^ b</p>
<p>python 特有的</p>
<p>a,b=b,a</p>
<h3 id="冒泡排序"><a href="#冒泡排序" class="headerlink" title="冒泡排序"></a>冒泡排序</h3><figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment"># 让一个数字与其下一个数字数字比较，前大于后则交换位置</span></span><br><span class="line"><span class="comment">#  发现盲点，原来冒泡不止一个泡，看来我以前都错了</span></span><br><span class="line"><span class="comment"># 下面我们做一个案例</span></span><br><span class="line">nums = [<span class="number">4</span>, <span class="number">0</span>, <span class="number">3</span>, <span class="number">5</span>, <span class="number">6</span>, <span class="number">2</span>, <span class="number">7</span>, <span class="number">1</span>, <span class="number">9</span>, <span class="number">8</span>]</span><br><span class="line"><span class="comment"># 设定循环变量，确定总趟数</span></span><br><span class="line">i = <span class="number">0</span></span><br><span class="line"><span class="keyword">while</span> i &lt; len(nums) - <span class="number">1</span>:</span><br><span class="line">    i += <span class="number">1</span></span><br><span class="line">    <span class="comment"># 下面的while循环实现了一次冒泡，及趟了一次</span></span><br><span class="line">    n = <span class="number">0</span></span><br><span class="line">    <span class="comment"># n是下标，下标过了一遍</span></span><br><span class="line">    <span class="keyword">while</span> n &lt; len(nums) - <span class="number">1</span>:</span><br><span class="line">        <span class="comment"># 下面的模块实现，满足要求的变量的位置交换</span></span><br><span class="line">        <span class="keyword">if</span> nums[n] &gt; nums[n + <span class="number">1</span>]:</span><br><span class="line">            nums[n], nums[n + <span class="number">1</span>] = nums[n + <span class="number">1</span>], nums[n]</span><br><span class="line">        n = n + <span class="number">1</span></span><br><span class="line">print(nums)</span><br><span class="line"><span class="comment"># 待优化：每一趟比较次数，总比较趟数，当前趟数为81趟</span></span><br></pre></td></tr></table></figure>

<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment"># 让一个数字与其下一个数字数字比较，前大于后则交换位置</span></span><br><span class="line"><span class="comment">#  发现盲点，原来冒泡不止一个泡，看来我以前都错了</span></span><br><span class="line"><span class="comment"># 下面我们做一个案例</span></span><br><span class="line">nums = [<span class="number">4</span>, <span class="number">0</span>, <span class="number">3</span>, <span class="number">5</span>, <span class="number">6</span>, <span class="number">2</span>, <span class="number">7</span>, <span class="number">1</span>, <span class="number">9</span>, <span class="number">8</span>]</span><br><span class="line">count = <span class="number">0</span></span><br><span class="line"><span class="comment"># 设定循环变量，确定总趟数</span></span><br><span class="line">i = <span class="number">0</span></span><br><span class="line"><span class="keyword">while</span> i &lt; len(nums) - <span class="number">1</span>:</span><br><span class="line">    <span class="comment"># 下面的while循环实现了一次冒泡，及趟了一次</span></span><br><span class="line">    n = <span class="number">0</span></span><br><span class="line">    <span class="comment"># n是下标，下标过了一遍</span></span><br><span class="line">    <span class="keyword">while</span> n &lt; len(nums) - <span class="number">1</span> -i:</span><br><span class="line">        count += <span class="number">1</span></span><br><span class="line">        <span class="comment"># 下面的模块实现，满足要求的变量的位置交换</span></span><br><span class="line">        <span class="keyword">if</span> nums[n] &gt; nums[n + <span class="number">1</span>]:</span><br><span class="line">            nums[n], nums[n + <span class="number">1</span>] = nums[n + <span class="number">1</span>], nums[n]</span><br><span class="line">        n = n + <span class="number">1</span></span><br><span class="line">    i += <span class="number">1</span></span><br><span class="line">print(nums)</span><br><span class="line">print(<span class="string">'总趟数'</span>,count)</span><br><span class="line"><span class="comment"># 待优化：每一趟比较次数，总比较趟数</span></span><br><span class="line"><span class="comment"># 每比一次少一趟,优化后的总趟数为了45趟</span></span><br></pre></td></tr></table></figure>

<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment"># 让一个数字与其下一个数字数字比较，前大于后则交换位置</span></span><br><span class="line"><span class="comment">#  发现盲点，原来冒泡不止一个泡，看来我以前都错了</span></span><br><span class="line"><span class="comment"># 下面我们做一个案例</span></span><br><span class="line">nums = [<span class="number">4</span>, <span class="number">0</span>, <span class="number">3</span>, <span class="number">5</span>, <span class="number">6</span>, <span class="number">2</span>, <span class="number">7</span>, <span class="number">1</span>, <span class="number">9</span>, <span class="number">8</span>]</span><br><span class="line">count = <span class="number">0</span></span><br><span class="line"><span class="comment"># 设定循环变量，确定总趟数</span></span><br><span class="line">i = <span class="number">0</span></span><br><span class="line"><span class="keyword">while</span> i &lt; len(nums) - <span class="number">1</span>:</span><br><span class="line">    <span class="comment"># 下面的while循环实现了一次冒泡，及趟了一次</span></span><br><span class="line">    n = <span class="number">0</span></span><br><span class="line">    <span class="comment"># n是下标，下标过了一遍</span></span><br><span class="line">    <span class="keyword">while</span> n &lt; len(nums) - <span class="number">1</span> -i:</span><br><span class="line">        count += <span class="number">1</span></span><br><span class="line">        flag = <span class="literal">True</span>  <span class="comment"># 假设每一趟都没有数据交换</span></span><br><span class="line">        <span class="comment"># 下面的模块实现，满足要求的变量的位置交换</span></span><br><span class="line">        <span class="keyword">if</span> nums[n] &gt; nums[n + <span class="number">1</span>]:</span><br><span class="line">            flag=<span class="literal">False</span></span><br><span class="line">            nums[n], nums[n + <span class="number">1</span>] = nums[n + <span class="number">1</span>], nums[n]</span><br><span class="line">        n = n + <span class="number">1</span></span><br><span class="line">    <span class="keyword">if</span> flag :</span><br><span class="line">        <span class="keyword">break</span></span><br><span class="line">    i += <span class="number">1</span></span><br><span class="line">print(nums)</span><br><span class="line">print(<span class="string">'总趟数'</span>,count)</span><br><span class="line"><span class="comment"># 待优化：每一趟比较次数，总比较趟数</span></span><br><span class="line"><span class="comment"># 每比一次少一趟,加入了假设成立项，总趟数减少到了17次</span></span><br></pre></td></tr></table></figure>

<h3 id="列表的排序和反转"><a href="#列表的排序和反转" class="headerlink" title="列表的排序和反转"></a>列表的排序和反转</h3><p>sort() 方法</p>
<p>sorted() 方法</p>
<p>reverse() 方法，仅仅反转，不做排序</p>
<p>valuename[::-1] 切片语法实现反转</p>
<h3 id="可变类型与不可变类型"><a href="#可变类型与不可变类型" class="headerlink" title="可变类型与不可变类型"></a>可变类型与不可变类型</h3><p>可变类型：列表、字典、集合</p>
<p>不可变类型：字符串、数字、元组</p>
<p>不可变类型如果修改值，内存地址会发生变化</p>
<p>使用内置函数 id() 可以获得变量在内存中的地址</p>
<h4 id="列表的复制"><a href="#列表的复制" class="headerlink" title="列表的复制"></a>列表的复制</h4><p>使用 copy() 方法，获得原有列表相同元素，但指向不同的内存空间</p>
<p>使用内置模块 import copy</p>
<p>​    valuename = copy.copy(object)  #  浅拷贝</p>
<p>​    切片是浅拷贝</p>
<p>深拷贝，先不讲</p>
<h5 id="重构flag"><a href="#重构flag" class="headerlink" title="重构flag:"></a>重构flag:</h5><p>对变量名双击，refactor -&gt; rename 即可当前变量全局重命名</p>
<h3 id="求列表里的最大数"><a href="#求列表里的最大数" class="headerlink" title="求列表里的最大数"></a>求列表里的最大数</h3><p>主要应用于数字列表</p>
<ol>
<li>排序后选择规则的最大数</li>
<li>使用max方法</li>
<li>假设一个最大数，去和每个数比较</li>
</ol>
<h3 id="Python删除元素"><a href="#Python删除元素" class="headerlink" title="Python删除元素"></a>Python删除元素</h3><p>参考Python啃书笔记</p>
<h3 id="列表的嵌套"><a href="#列表的嵌套" class="headerlink" title="列表的嵌套"></a>列表的嵌套</h3><p>在C语言中应该类似多维数组</p>
<p>新方法，random.choice(object)随机选择</p>
<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">import</span> random  <span class="comment"># 引入随机模块</span></span><br><span class="line"><span class="comment"># 十个老师，三个办公室</span></span><br><span class="line">teachers = [<span class="string">'A'</span>, <span class="string">'B'</span>, <span class="string">'C'</span>, <span class="string">'D'</span>, <span class="string">'E'</span>, <span class="string">'F'</span>, <span class="string">'G'</span>, <span class="string">'H'</span>, <span class="string">'I'</span>, <span class="string">'J'</span>]</span><br><span class="line">rooms = [[],[],[]]</span><br><span class="line"><span class="keyword">for</span> teacher <span class="keyword">in</span> teachers :</span><br><span class="line">    room=random.choice(rooms)</span><br><span class="line">    room.append(teacher)</span><br><span class="line">print(rooms)</span><br></pre></td></tr></table></figure>

<p>带下标一般使用while循环，for循环的话使用多变量参与迭代遍历，结合 enumerate() 函数，</p>
<h3 id="列表推导式的使用"><a href="#列表推导式的使用" class="headerlink" title="列表推导式的使用"></a>列表推导式的使用</h3><p>仿佛记得前面学过一个类似的使用方法，三元表达式！</p>
<p>作用是使用简单的语法创建一个列表</p>
<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br></pre></td><td class="code"><pre><span class="line">nums = [i <span class="keyword">for</span> i <span class="keyword">in</span> range(<span class="number">10</span>)]</span><br><span class="line"><span class="comment"># 定义一个循环，生成列表元素</span></span><br><span class="line">print(nums)  <span class="comment"># [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]</span></span><br><span class="line"></span><br><span class="line">x = [i <span class="keyword">for</span> i <span class="keyword">in</span> range(<span class="number">10</span>) <span class="keyword">if</span> i % <span class="number">2</span>]</span><br><span class="line">print(x)  <span class="comment"># [1, 3, 5, 7, 9]</span></span><br><span class="line"></span><br><span class="line"><span class="comment"># 两个元素</span></span><br><span class="line">points = [(x, y) <span class="keyword">for</span> x <span class="keyword">in</span> range(<span class="number">5</span>, <span class="number">9</span>) <span class="keyword">for</span> y <span class="keyword">in</span> range(<span class="number">10</span>, <span class="number">20</span>)]</span><br><span class="line">print(points)</span><br><span class="line"><span class="comment"># [(5, 10), (5, 11), (5, 12), (5, 13), (5, 14), (5, 15), (5, 16), (5, 17), (5, 18), (5, 19), (6, 10), (6, 11), (6, 12), (6, 13), (6, 14), (6, 15), (6, 16), (6, 17), (6, 18), (6, 19), (7, 10), (7, 11), (7, 12), (7, 13), (7, 14), (7, 15), (7, 16), (7, 17), (7, 18), (7, 19), (8, 10), (8, 11), (8, 12), (8, 13), (8, 14), (8, 15), (8, 16), (8, 17), (8, 18), (8, 19)]</span></span><br><span class="line"></span><br><span class="line"><span class="comment"># 切片</span></span><br><span class="line">n = [nums[j:j + <span class="number">3</span>] <span class="keyword">for</span> j <span class="keyword">in</span> range(<span class="number">0</span>, <span class="number">10</span>, <span class="number">3</span>)]</span><br><span class="line"><span class="comment"># range() 也能加步长这个参数</span></span><br><span class="line">print(n)  <span class="comment"># [[0, 1, 2], [3, 4, 5], [6, 7, 8], [9]]</span></span><br></pre></td></tr></table></figure>

<h3 id="浅拷贝与深拷贝"><a href="#浅拷贝与深拷贝" class="headerlink" title="浅拷贝与深拷贝"></a>浅拷贝与深拷贝</h3><figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">import</span> copy</span><br><span class="line"></span><br><span class="line">nums = [<span class="number">1</span>, <span class="number">2</span>, <span class="number">3</span>, <span class="number">4</span>, <span class="number">5</span>]</span><br><span class="line">nums1 = nums  <span class="comment"># 不是深拷贝也不是浅拷贝，是一个指向，是赋值</span></span><br><span class="line">nums2 = nums.copy()  <span class="comment"># 浅拷贝，内容相同，对象不同</span></span><br><span class="line">nums3 = copy.copy(nums)  <span class="comment"># 和nums.copy功能一致，都是浅拷贝</span></span><br><span class="line"></span><br><span class="line">words = [<span class="string">'hello'</span>, <span class="string">'good'</span>, [<span class="number">100</span>, <span class="number">200</span>, <span class="number">300</span>], <span class="string">'yes'</span>, <span class="string">'hi'</span>, <span class="string">'ok'</span>]</span><br><span class="line">words1 = words.copy()  <span class="comment"># 浅拷贝，内存不同，对象独立</span></span><br><span class="line"><span class="comment"># 浅拷贝：一维拷贝，多维指向，即修改words[2],words1也会改变</span></span><br><span class="line"><span class="comment"># words[2][1] = 1  # 为了不影响words2 注释掉了</span></span><br><span class="line">print(<span class="string">'words1'</span>, words1)</span><br><span class="line"><span class="comment"># words1 ['hello', 'good', [100, 200, 300], 'yes', 'hi', 'ok']</span></span><br><span class="line"></span><br><span class="line"><span class="comment"># 深拷贝，只能用copy模块实现，涉及到列表嵌套</span></span><br><span class="line">words2 = copy.deepcopy(words)</span><br><span class="line">words[<span class="number">2</span>][<span class="number">1</span>] = <span class="string">'words2'</span></span><br><span class="line">print(<span class="string">'words'</span>, words)</span><br><span class="line">print(<span class="string">'words2'</span>, words2)</span><br><span class="line"><span class="comment"># words ['hello', 'good', [100, 'words2', 300], 'yes', 'hi', 'ok']</span></span><br><span class="line"><span class="comment"># words2 ['hello', 'good', [100, 200, 300], 'yes', 'hi', 'ok']</span></span><br><span class="line"><span class="comment"># 做到了多维拷贝，全部实现单纯复制</span></span><br></pre></td></tr></table></figure>

<h3 id="元组"><a href="#元组" class="headerlink" title="元组"></a>元组</h3><p>有序、不可变数据类型</p>
<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment"># 元组与列表的区别：元组是不可变数据类型，只能查询</span></span><br><span class="line"><span class="comment"># 两个方法 count()统计出现了几次， index()查询下标是多少</span></span><br><span class="line"><span class="comment"># 特殊情况，如何表现只有一个数据的元组</span></span><br><span class="line"><span class="comment"># 不能用ages=(18)</span></span><br><span class="line">ages = (<span class="number">18</span>,)  <span class="comment"># 关键符号是这一个逗号</span></span><br><span class="line">print(type(ages))</span><br><span class="line"><span class="comment"># &lt;class 'tuple'&gt;</span></span><br><span class="line"><span class="comment"># tuple 内置类</span></span><br><span class="line">print(tuple(<span class="string">'hello'</span>))  <span class="comment"># ('h', 'e', 'l', 'l', 'o')</span></span><br></pre></td></tr></table></figure>

<h4 id="列表与元组的相互转换"><a href="#列表与元组的相互转换" class="headerlink" title="列表与元组的相互转换"></a>列表与元组的相互转换</h4><p>tuple() 参数是可迭代对象</p>
<p>list() 参数是可迭代对象</p>
<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment"># 列表转为元组</span></span><br><span class="line">words = [<span class="string">'saka'</span>, <span class="string">'naraii'</span>, <span class="string">'hello'</span>, <span class="string">'nike'</span>]</span><br><span class="line">print(tuple(words))  <span class="comment"># ('saka', 'naraii', 'hello', 'nike')</span></span><br><span class="line">print(list(tuple(words)))  <span class="comment"># ['saka', 'naraii', 'hello', 'nike']</span></span><br></pre></td></tr></table></figure>

<h3 id="join-函数"><a href="#join-函数" class="headerlink" title="join()函数"></a>join()函数</h3><p>​    语法： ‘sep’.join(seq)</p>
<ul>
<li>sep：分隔符。可以为空</li>
<li>seq：要连接的元素序列、字符串、元组、字典</li>
</ul>
<h3 id="字典的使用"><a href="#字典的使用" class="headerlink" title="字典的使用"></a>字典的使用</h3><p>列表一般存储同类型数据，混合数据类型使用字典，字典的数据以键-值对的方式来存储，后续会学到嵌套，字典里的 key 不允许重复，否则后面的值会覆盖前一个。key 只能是不可变数据类型，value可以是任意数据类型</p>
<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br></pre></td><td class="code"><pre><span class="line">person = &#123;</span><br><span class="line">    <span class="string">'name'</span>: <span class="string">'zhangsan'</span>,</span><br><span class="line">    <span class="string">'age'</span>: <span class="number">18</span>,</span><br><span class="line">    <span class="string">'math'</span>: <span class="number">99</span>,</span><br><span class="line">    <span class="string">'Chinese'</span>: <span class="number">99</span>,</span><br><span class="line">    <span class="string">'Japanese'</span>: <span class="number">99</span>,</span><br><span class="line">    <span class="string">'English'</span>: <span class="number">99</span>,</span><br><span class="line">    <span class="comment"># 我在想peach</span></span><br><span class="line">    <span class="string">'isPass'</span>: <span class="literal">True</span>,</span><br><span class="line">    <span class="string">'hobbies'</span>: [<span class="string">'sing'</span>, <span class="string">'jump'</span>, <span class="string">'rap'</span>, <span class="string">'basketball'</span>]</span><br><span class="line">    <span class="number">2020</span>: <span class="string">'good'</span>,</span><br><span class="line">    (<span class="string">'really'</span>,): <span class="literal">True</span>,</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<h4 id="字典的增删改查"><a href="#字典的增删改查" class="headerlink" title="字典的增删改查"></a>字典的增删改查</h4><p>查询：字典是无序的，不能通过下标查询，可以通过 key，key 不存在会报错。因为唯一性的原因，并不能通过 value 来寻找key。</p>
<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br></pre></td><td class="code"><pre><span class="line">person = &#123;</span><br><span class="line">    <span class="string">'name'</span>: <span class="string">'zhangsan'</span>,</span><br><span class="line">    <span class="string">'age'</span>: <span class="number">18</span>,</span><br><span class="line">&#125;</span><br><span class="line">print(person[<span class="string">'age'</span>])  <span class="comment"># 18</span></span><br><span class="line">print(person.get(<span class="string">'hobby'</span>, <span class="string">'rap'</span>))  <span class="comment"># None</span></span><br><span class="line"><span class="comment"># 查询不存在的键会报错</span></span><br><span class="line"><span class="comment"># 使用get() 方法可以避免报错，如果不存在则返回 None</span></span><br><span class="line"><span class="comment"># (若给定第二个参数，返回第二个参数，不会修改原字典数据)</span></span><br></pre></td></tr></table></figure>

<p>修改与增加</p>
<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br></pre></td><td class="code"><pre><span class="line">person = &#123;</span><br><span class="line">    <span class="string">'name'</span>: <span class="string">'zhangsan'</span>,</span><br><span class="line">    <span class="string">'age'</span>: <span class="number">18</span>,</span><br><span class="line">&#125;</span><br><span class="line"><span class="comment"># 直接使用key可以修改对应的value</span></span><br><span class="line">person[<span class="string">'name'</span>] = <span class="string">'lisi'</span></span><br><span class="line">print(person[<span class="string">'name'</span>])  <span class="comment"># lisi</span></span><br><span class="line"><span class="comment"># 如果存在则修改，不存在则新添加一个键-值对</span></span><br><span class="line">person[<span class="string">'hobby'</span>] = <span class="string">'rap'</span></span><br><span class="line">print(person)  <span class="comment"># &#123;'name': 'lisi', 'age': 18, 'hobby': 'rap'&#125;</span></span><br></pre></td></tr></table></figure>

<p>删除</p>
<p>pop() 方法，执行结果是被删除的value</p>
<p>popitem() 方法，删除一个元素，结果是这个元素组成的键值对的元组</p>
<p>clear() 方法，清空一个字典，</p>
<p>del valuename[‘key’] 删除语句</p>
<h4 id="update方法的使用"><a href="#update方法的使用" class="headerlink" title="update方法的使用"></a>update方法的使用</h4><p>类似于列表合并方法 extend()</p>
<p>可以将两个字典合并成为一个字典</p>
<p>字典之间不支持加法运算</p>
<p>valuename1.update(valuename2) 结果是合并到了valuename1</p>
<h3 id="字典的遍历"><a href="#字典的遍历" class="headerlink" title="字典的遍历"></a>字典的遍历</h3><figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br></pre></td><td class="code"><pre><span class="line">person = &#123;</span><br><span class="line">    <span class="string">'name'</span>: <span class="string">'zhangsan'</span>,</span><br><span class="line">    <span class="string">'age'</span>: <span class="number">18</span>,</span><br><span class="line">    <span class="string">'height'</span>: <span class="string">'180cm'</span>,</span><br><span class="line">&#125;</span><br><span class="line"><span class="comment"># 遍历方式1</span></span><br><span class="line"><span class="comment"># 字典是键-值对的形式存在</span></span><br><span class="line"><span class="keyword">for</span> value <span class="keyword">in</span> person:</span><br><span class="line">    print(value)</span><br><span class="line">    <span class="comment"># 返回的是 key</span></span><br><span class="line"><span class="comment"># 遍历方式2 和方式1一样，拿到的就是键</span></span><br><span class="line"><span class="keyword">for</span> k <span class="keyword">in</span> person.keys():</span><br><span class="line">    print(k)</span><br><span class="line"><span class="comment"># 遍历方式3，只拿值</span></span><br><span class="line"><span class="keyword">for</span> var <span class="keyword">in</span> person.values():</span><br><span class="line">    print(var)</span><br><span class="line"><span class="comment"># 遍历方式4，键-值对全拿，每次拿一项</span></span><br><span class="line"><span class="comment"># 一个item表示一个项，每项以元组的方式返回</span></span><br><span class="line"><span class="keyword">for</span> item <span class="keyword">in</span> person.items():</span><br><span class="line">    print(item)</span><br><span class="line"><span class="comment"># 改进为多变量循环，就是解构，拆包</span></span><br><span class="line"><span class="keyword">for</span> k, v <span class="keyword">in</span> person.items():</span><br><span class="line">    print(k, v)</span><br></pre></td></tr></table></figure>

<h4 id="字典推导式之键值颠倒"><a href="#字典推导式之键值颠倒" class="headerlink" title="字典推导式之键值颠倒"></a>字典推导式之键值颠倒</h4><figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br></pre></td><td class="code"><pre><span class="line">dict1 = &#123;</span><br><span class="line">    <span class="string">'a'</span>: <span class="number">100</span>,</span><br><span class="line">    <span class="string">'b'</span>: <span class="number">200</span>,</span><br><span class="line">    <span class="string">'c'</span>: <span class="number">300</span>,</span><br><span class="line">&#125;</span><br><span class="line">dict2 = &#123;&#125;</span><br><span class="line"><span class="keyword">for</span> key, value <span class="keyword">in</span> dict1.items():</span><br><span class="line">    dict2[value] = key</span><br><span class="line">    <span class="comment"># 啥也没有，见一个加入一个</span></span><br><span class="line">print(dict2)</span><br><span class="line"><span class="comment"># &#123;100: 'a', 200: 'b', 300: 'c'&#125;</span></span><br><span class="line"></span><br><span class="line"><span class="comment"># 字典推导式</span></span><br><span class="line">dict1 = &#123;v: k <span class="keyword">for</span> k, v <span class="keyword">in</span> dict1.items()&#125;</span><br><span class="line">print(dict1)</span><br><span class="line"><span class="comment"># &#123;100: 'a', 200: 'b', 300: 'c'&#125;</span></span><br></pre></td></tr></table></figure>

<h4 id="案例之作业讲解"><a href="#案例之作业讲解" class="headerlink" title="案例之作业讲解"></a>案例之作业讲解</h4><p>省略</p>
<h3 id="集合的基本使用"><a href="#集合的基本使用" class="headerlink" title="集合的基本使用"></a>集合的基本使用</h3><p>使用set（）表示空集合，{}表示空字典</p>
<p><strong>常用方法注意：</strong></p>
<p>​    pop()方法，随机弹出一个</p>
<p>​    remove（指定元素），若元素不存在则报错KeyError</p>
<p>​    union（）将多个集合合并成新集合，可设置变量接收一下</p>
<p>​    update（）将新的集合合并到源集合，不产生新的集合，参数为可迭代对象</p>
<h4 id="集合运算符的使用"><a href="#集合运算符的使用" class="headerlink" title="集合运算符的使用"></a>集合运算符的使用</h4><p>set支持很多算术运算符</p>
<p><strong>减法-求差集：</strong></p>
<p>可以使用减法，得到两个集合的差集（多元素减少元素可得到差集，否则差集为空集）</p>
<p><strong>按位于&amp;求交集：</strong></p>
<p>求两个集合的交集，也存在交集为空集的情况。</p>
<p><strong>按位或 | 求并集：</strong></p>
<p><strong>按位异或 ^ 求差集的并集（并集减去交集）：</strong></p>
<h4 id="集合的练习，去重排序"><a href="#集合的练习，去重排序" class="headerlink" title="集合的练习，去重排序"></a>集合的练习，去重排序</h4><p>使用set（）根据可迭代对象建立集合，结合列表使用有序排列，之后操作列表即可，因为直接对集合排序不太好，。。。集合是无序的。</p>
<h4 id="使用内置类进行数据转化"><a href="#使用内置类进行数据转化" class="headerlink" title="使用内置类进行数据转化"></a>使用内置类进行数据转化</h4><p>使用以下方法可实现数据的类型转换，注意字典是键值对，转成其他只会保留key</p>
<p>list（）、tuple（）、set（）、dict（）</p>
<h4 id="eval-直接执行语法字符串的内容"><a href="#eval-直接执行语法字符串的内容" class="headerlink" title="eval()直接执行语法字符串的内容"></a>eval()直接执行语法字符串的内容</h4><p>eval（）, 如果字符串内时符合Python语法的代码时，可以使用eval执行</p>
<p>a=‘input（“GG”）’</p>
<p>eval(a)</p>
<h4 id="json-将其他数据类型转换成json字符串"><a href="#json-将其他数据类型转换成json字符串" class="headerlink" title="json() 将其他数据类型转换成json字符串"></a>json() 将其他数据类型转换成json字符串</h4><p>使用带s的来进行格式转换。（loads、dumps）</p>
<p>dumps()转换字符串，转换后都加双引号，数字可加可不加，转换后的单引号并不会显示出来，所以看起来像字典，本质是json字符串，便不可使用字典的访问方式</p>
<p>loads()将json字符串，转换为对应数据类型（另外也可以使用eval()函数）</p>
<p>列表和元组可以使用loads（）转换，字典不行</p>
<h3 id="公共方法"><a href="#公共方法" class="headerlink" title="公共方法"></a>公共方法</h3><p>加法用于拼接，字符串，元组，列表</p>
<p>减法只能用于集合求差集</p>
<p>乘法，重复元素多次，不能用于字典和集合</p>
<p>成员运算符，in 、not in 用来判断元素是否存在于可迭代对象</p>
<p>字典用in的话，只作用于key。</p>
<p><strong>带下标的遍历enumerate（）方法，返回结果是（下标，数据）</strong></p>
<p>对于字典的话是（下标，key），一般使用items（）来遍历字典</p>
<h3 id="函数"><a href="#函数" class="headerlink" title="函数"></a>函数</h3><p>def 函数名（参数）：</p>
<p>​    函数体</p>
<p>调用函数：函数名（参数）</p>
<p>形参的值是不确定的，只是用来占位置</p>
<h4 id="传参的方式"><a href="#传参的方式" class="headerlink" title="传参的方式"></a>传参的方式</h4><p>位置对应，提供变量名赋值，混合传参</p>
<h4 id="返回值"><a href="#返回值" class="headerlink" title="返回值"></a>返回值</h4><p>并不是所有函数都需要返回值</p>
<p>使用return语句返回</p>
<p>多值返回可使用列表、字典等</p>
<h4 id="函数的注释"><a href="#函数的注释" class="headerlink" title="函数的注释"></a>函数的注释</h4><p>“””大概写好基本功能后，加个回车，函数说明”””</p>
<p>参数的类型是没办法限制的，可以使用冒号进行建议，像a: int，表示建议向a传入int数据类型，但是传入其他数据类型也不一定会报错，所以存在风险。</p>
<h4 id="函数的调用"><a href="#函数的调用" class="headerlink" title="函数的调用"></a>函数的调用</h4><p>要在这里学会使用端点调试，</p>
<p>单步运行时，断点后的函数会直接执行，emmm多用用就熟练了。</p>
<p>如果想进入断点之后的函数可以使用step into（步进？）功能。出来时用step out功能即可</p>
<h4 id="全局变量和局部变量"><a href="#全局变量和局部变量" class="headerlink" title="全局变量和局部变量"></a>全局变量和局部变量</h4><p>整个py文件下为全局变量，可以在文件内任意处使用；函数内部为局部变量，只能在内部使用；</p>
<p>如何在函数内部修改全局变量：</p>
<p>使用global关键字</p>
<p>global valuename</p>
<p>valuename = new_value</p>
<h5 id="内置函数globals（）-和-locals（）"><a href="#内置函数globals（）-和-locals（）" class="headerlink" title="内置函数globals（） 和 locals（）"></a>内置函数globals（） 和 locals（）</h5><p>可以查看全局变量和局部变量。</p>
<p>对于整个文件来说，函数定义在文件内的函数也是全局变量</p>
<p>在Python里，只有函数可以分割作用域</p>
<p>如果在if语句内定义变量的话，也是全局变量，但是条件不成立的话，会报错变量没有定义。！</p>
<h5 id="多值返回"><a href="#多值返回" class="headerlink" title="多值返回"></a>多值返回</h5><p>finally（）语句中，会出现执行多个return的情况</p>
<p>一般情况下，使用列表等可迭代数据类型进行返回。</p>
<p>一般使用元组进行返回，可以使用不带括号的形式</p>
<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment"># 拆包解包</span></span><br><span class="line">x, y = (<span class="string">'hello'</span>, <span class="string">'world'</span>)</span><br><span class="line">print(x,y)</span><br></pre></td></tr></table></figure>

<h3 id="函数回顾"><a href="#函数回顾" class="headerlink" title="函数回顾"></a>函数回顾</h3><ol>
<li>函数声明 def</li>
<li>函数格式 def 函数名（形参。。。）</li>
<li>函数的调用 函数名（实参。。。）</li>
<li>函数的返回值，使用return语句返回函数的执行结果</li>
<li>返回多个结果，学会打包解包，通常使用元组</li>
<li>函数名也是一个标识符，遵守命名规范以及顾名思义</li>
</ol>
<h3 id="函数的缺省参数"><a href="#函数的缺省参数" class="headerlink" title="函数的缺省参数"></a>函数的缺省参数</h3><p>缺省参数实际就是事先提供好了参数值，声明形参的时候提供默认值。</p>
<p>关键字参数放在位置参数的后面。</p>
<p>调用时需要将位置参数写在前面。</p>
<h3 id="可变参数"><a href="#可变参数" class="headerlink" title="可变参数"></a>可变参数</h3><p>一个星号 以元组的方式存储，表示可变的位置参数，*args</p>
<p>两个星好 以字典的方式存储，表示可变的关键字参数，  **kwargs</p>
<p>参数在声明时候需要放在缺省参数前面，总之就是缺省参数要放到最后</p>
<h3 id="可变类型和不可变数据类型"><a href="#可变类型和不可变数据类型" class="headerlink" title="可变类型和不可变数据类型"></a>可变类型和不可变数据类型</h3><p>在这一章节的话，就是传递数据过去的时候，是否允许修改。</p>
<p>还涉及到了，深拷贝与浅拷贝。</p>
<h4 id="函数的注意事项"><a href="#函数的注意事项" class="headerlink" title="函数的注意事项"></a>函数的注意事项</h4><p>函数的三要素，函数名，参数，返回值</p>
<p>Python里不允许函数重名，如果重名，则后覆盖前。</p>
<p>函数名与变量名也不要重复，可以把函数理解为当前文件下的一个变量。</p>
<h4 id="递归函数的使用"><a href="#递归函数的使用" class="headerlink" title="递归函数的使用"></a>递归函数的使用</h4><p>函数自己调自己！</p>
<p>程序对递归深度有限制</p>
<p>递归最重要的就是找到出口（停止的条件）：</p>
<p>​    可以使用global结合计数barabara</p>
<p>​    在递归里不用break，break只用于循环语句 </p>
<h4 id="匿名函数"><a href="#匿名函数" class="headerlink" title="匿名函数"></a>匿名函数</h4><p>首先理解下划定别名，就像导入模块时的as：</p>
<ul>
<li>将函数理解为一块准备好的代码，在内存中占有空间。类似于函数名指向了含有一段代码的内存空间。</li>
<li>使用新的变量名，赋值为函数名，那么这个新的变量名就会指向原函数的内存。相当于给原函数定义了一个别名。</li>
</ul>
<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br></pre></td><td class="code"><pre><span class="line"><span class="function"><span class="keyword">def</span> <span class="title">say_hello</span><span class="params">()</span>:</span></span><br><span class="line">    print(<span class="string">'Hello'</span>)</span><br><span class="line"></span><br><span class="line"></span><br><span class="line">say_hello()</span><br><span class="line">hi = say_hello</span><br><span class="line">hi()</span><br><span class="line"><span class="comment"># Hello</span></span><br><span class="line"><span class="comment"># Hello</span></span><br></pre></td></tr></table></figure>

<p>除了使用def关键字定义一个函数以外，我们也可以使用lambda表达式定义一个匿名函数。多用于表达简单函数，调用次数很少，基本就是一次。</p>
<p>语法如下：</p>
<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">lambda</span> a, b: print(a + b)</span><br></pre></td></tr></table></figure>

<p>调用匿名函数方式有两种：</p>
<ol>
<li><p>不常用，给它起一个名字，使用赋值方式指定别名</p>
<ol>
<li><pre><code class="Python">get_sum = <span class="keyword">lambda</span> a, b: print(a + b) 
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br></pre></td><td class="code"><pre><span class="line"></span><br><span class="line">2. 将其作为参数传给另一个函数使用</span><br><span class="line"></span><br><span class="line">   1. 主要就是作为参数的时候不用写括号</span><br><span class="line"></span><br><span class="line">   2. 事先定义好，作为参数使用的时候省去括号，也许这就是匿了个括号吧</span><br><span class="line"></span><br><span class="line">   3. 开发中称为回调函数，属于函数的高级应用</span><br><span class="line"></span><br><span class="line">   4. 结合这个匿名函数表达式的话就是写在一行</span><br><span class="line"></span><br><span class="line">      1. 就像：x1 &#x3D; calc(5,7,lambda x,y:x+y)</span><br><span class="line"></span><br><span class="line">   5. 就像作为参数的那个函数在此处被展开了，而且没有名字，就用一次，使用lambda匿名</span><br><span class="line"></span><br><span class="line">      1. </span><br><span class="line"></span><br><span class="line">      &#96;&#96;&#96;Python</span><br><span class="line">      def clac(x, y, fn):</span><br><span class="line">          # fn是一个变量名，指向了匿名函数</span><br><span class="line">          return fn(x, y)</span><br><span class="line">      </span><br><span class="line">      </span><br><span class="line">      x1 &#x3D; clac(1, 2, lambda a, b: a + b)</span><br><span class="line">      # lambda a, b: a + b这一大块就是匿名定义的fn，而且fn只是一个别名</span><br><span class="line">      print(x1)</span><br></pre></td></tr></table></figure>
</code></pre>
</li>
</ol>
</li>
</ol>
<h3 id="列表相关的一些方法"><a href="#列表相关的一些方法" class="headerlink" title="列表相关的一些方法"></a>列表相关的一些方法</h3><p>方法obj.sort（）永久排序，对原列表进行改动</p>
<p>内置函数sorted（obj）临时排序，生成新的列表</p>
<p>sort（）方法接受key参数时，可以指定比较规则，但该规则是一个函数。</p>
<p> students.sort(key = lambda v_name:v_name[‘key_name’])</p>
<p>意思就是让一个存有学生各种信息的嵌套字典列表根据学生们的分数来排序，这里的key的参数还是匿名参数，这个匿名参数的作用就是返回对应键的值。</p>
<p>filter（）函数，对可迭代对象进行过滤，结果得到一个filter对象，是可迭代的。</p>
<p>map（）函数，将可迭代对象里的每个数据但规则进行变化。</p>
<p>reduce（）函数，减项，求和，具体使用方法直接看文档。另外，第三个参数是，初始化值第一项的值。貌似在求嵌套数据时（例如嵌套字典的列表）十分有用。</p>
<h3 id="内置函数的总结"><a href="#内置函数的总结" class="headerlink" title="内置函数的总结"></a>内置函数的总结</h3><p>具体可见builtins.py文档</p>
<p>数学相关：</p>
<ul>
<li>abs（）取绝对值</li>
<li>bin（）将数字转为二进制</li>
<li>oct（）</li>
<li>hex（）</li>
<li>divmod（）返回包括商和余数的元组</li>
<li>max（）</li>
<li>min（）</li>
<li>pow（）幂运算</li>
<li>round（）四舍五入，保留到指定小数位</li>
<li>sum（）</li>
</ul>
<p>可迭代对象相关：</p>
<ul>
<li><p>all（）全部转为布尔值，取‘与’，为True</p>
</li>
<li><p>any（）全部转为布尔值，取‘或’True</p>
</li>
<li><p>len（）</p>
</li>
<li><p>iter（）获取到可迭代对象的迭代器</p>
</li>
<li><p>next：for in循环的本质就是不断调用迭代器的next方法</p>
</li>
<li><p>sorted（）</p>
</li>
</ul>
<p>数据操作：</p>
<ul>
<li><p>ascii（）将字符串或字符转为ascii码</p>
</li>
<li><p>chr（）将字符编码转换为对应字符</p>
</li>
<li><p>ord（）将字符转换成为对应的编码</p>
</li>
<li><p>dir（）列出这个对象能够用的所有方法！</p>
</li>
<li><p>exit（给定码）执行到这里就以给定码结束程序了</p>
</li>
<li><p>repr（）将对象变为字符串。以字符串方式显示数据</p>
</li>
</ul>
<p>变量相关：</p>
<ul>
<li>globals()与locals() 查看所有的全局与局部变量</li>
</ul>
<p>help（）用来查看帮助文档</p>
<p>id（）获取数据的内存地址</p>
<p>isinstance（）判断一个对象是否是由一个类创建出来的</p>
<p>issubclass（）判断一个类是否是另一个类的子类</p>
<p>open（）打开文件</p>
<h3 id="高阶函数"><a href="#高阶函数" class="headerlink" title="高阶函数"></a>高阶函数</h3><ol>
<li>一个函数作为另一个函数的返回值<ol>
<li>还会出现f_name()()</li>
<li>不带括号时，赋值就是取别名</li>
</ol>
</li>
<li>一个函数作为另一个函数的参数，详情见lambda</li>
<li>在函数内部再定义一个函数<ol>
<li>一般会将内部的函数作为返回值</li>
</ol>
</li>
</ol>
<h4 id="闭包的概念，应该归到高阶函数3"><a href="#闭包的概念，应该归到高阶函数3" class="headerlink" title="闭包的概念，应该归到高阶函数3"></a>闭包的概念，应该归到高阶函数3</h4><h5 id="闭包条件："><a href="#闭包条件：" class="headerlink" title="闭包条件："></a>闭包条件：</h5><ol>
<li>函数嵌套</li>
<li>外部函数返回内部函数</li>
<li>外部函数存在局部变量</li>
<li>内部函数使用到了这个局部变量<ol>
<li>内部使用同名变量是重新定义了一个变量，而不是使用外部函数的变量</li>
</ol>
</li>
</ol>
<p>内部函数不能通过global修改外部函数的局部变量，可以使用nonlocal关键字来修改外部函数的变量值。遵循先声明后修改的逻辑。</p>
<h5 id="时间戳"><a href="#时间戳" class="headerlink" title="时间戳"></a>时间戳</h5><p>从1970-01-01 00:00:00 UTC（时区） 到现在的秒数</p>
<h3 id="装饰器的使用"><a href="#装饰器的使用" class="headerlink" title="装饰器的使用"></a>装饰器的使用</h3><p>先记住装饰器的写法：有点闭包的意思</p>
<p>用法是在定义函数前一行加上 @装饰器名</p>
<p>只要被装饰了就会先调用一遍</p>

        </div>

        <blockquote class="post-copyright">
    
    <div class="content">
        
<span class="post-time">
    Last updated: <time datetime="2020-08-28T01:36:37.585Z" itemprop="dateUpdated">2020-08-28 09:36:37</time>
</span><br>


        
        这里可以写作者留言，标签和 hexo 中所有变量及辅助函数等均可调用，示例：<a href="/blog/2020/07/16/%E5%8D%83%E5%B3%B0%E6%95%99%E7%A8%8B%E7%AC%94%E8%AE%B0/" target="_blank" rel="external">https://gitee.com/songbug/blog.git/2020/07/16/%E5%8D%83%E5%B3%B0%E6%95%99%E7%A8%8B%E7%AC%94%E8%AE%B0/</a>
        
    </div>
    
    <footer>
        <a href="https://gitee.com/songbug/blog.git">
            <img src="/blog/img/avatar.jpg" alt="Song Wani">
            Song Wani
        </a>
    </footer>
</blockquote>

        
<div class="page-reward">
    <a id="rewardBtn" href="javascript:;" class="page-reward-btn waves-effect waves-circle waves-light">赏</a>
</div>



        <div class="post-footer">
            
	<ul class="article-tag-list" itemprop="keywords"><li class="article-tag-list-item"><a class="article-tag-list-link" href="/blog/tags/%E5%AD%A6%E4%B9%A0/" rel="tag">学习</a></li></ul>


            
<div class="page-share-wrap">
    

<div class="page-share" id="pageShare">
    <ul class="reset share-icons">
      <li>
        <a class="weibo share-sns" target="_blank" href="http://service.weibo.com/share/share.php?url=https://gitee.com/songbug/blog.git/2020/07/16/%E5%8D%83%E5%B3%B0%E6%95%99%E7%A8%8B%E7%AC%94%E8%AE%B0/&title=《千峰教程笔记》 — SongKaBaKa&pic=https://gitee.com/songbug/blog.git/img/avatar.jpg" data-title="微博">
          <i class="icon icon-weibo"></i>
        </a>
      </li>
      <li>
        <a class="weixin share-sns wxFab" href="javascript:;" data-title="微信">
          <i class="icon icon-weixin"></i>
        </a>
      </li>
      <li>
        <a class="qq share-sns" target="_blank" href="http://connect.qq.com/widget/shareqq/index.html?url=https://gitee.com/songbug/blog.git/2020/07/16/%E5%8D%83%E5%B3%B0%E6%95%99%E7%A8%8B%E7%AC%94%E8%AE%B0/&title=《千峰教程笔记》 — SongKaBaKa&source=" data-title=" QQ">
          <i class="icon icon-qq"></i>
        </a>
      </li>
      <li>
        <a class="facebook share-sns" target="_blank" href="https://www.facebook.com/sharer/sharer.php?u=https://gitee.com/songbug/blog.git/2020/07/16/%E5%8D%83%E5%B3%B0%E6%95%99%E7%A8%8B%E7%AC%94%E8%AE%B0/" data-title=" Facebook">
          <i class="icon icon-facebook"></i>
        </a>
      </li>
      <li>
        <a class="twitter share-sns" target="_blank" href="https://twitter.com/intent/tweet?text=《千峰教程笔记》 — SongKaBaKa&url=https://gitee.com/songbug/blog.git/2020/07/16/%E5%8D%83%E5%B3%B0%E6%95%99%E7%A8%8B%E7%AC%94%E8%AE%B0/&via=https://gitee.com/songbug/blog.git" data-title=" Twitter">
          <i class="icon icon-twitter"></i>
        </a>
      </li>
      <li>
        <a class="google share-sns" target="_blank" href="https://plus.google.com/share?url=https://gitee.com/songbug/blog.git/2020/07/16/%E5%8D%83%E5%B3%B0%E6%95%99%E7%A8%8B%E7%AC%94%E8%AE%B0/" data-title=" Google+">
          <i class="icon icon-google-plus"></i>
        </a>
      </li>
    </ul>
 </div>



    <a href="javascript:;" id="shareFab" class="page-share-fab waves-effect waves-circle">
        <i class="icon icon-share-alt icon-lg"></i>
    </a>
</div>



        </div>
    </div>

    
<nav class="post-nav flex-row flex-justify-between">
  
    <div class="waves-block waves-effect prev">
      <a href="/blog/2020/07/17/Python%E5%B8%B8%E7%94%A8%E7%9A%84%E6%96%B9%E6%B3%95%E5%8F%8A%E5%87%BD%E6%95%B0/" id="post-prev" class="post-nav-link">
        <div class="tips"><i class="icon icon-angle-left icon-lg icon-pr"></i> Prev</div>
        <h4 class="title">Python常用方法及函数</h4>
      </a>
    </div>
  

  
    <div class="waves-block waves-effect next">
      <a href="/blog/2020/07/10/%E5%AD%97%E5%85%B8%E4%B8%AD%E5%AD%98%E5%82%A8%E5%88%97%E8%A1%A8%E5%8F%8A%E5%A4%9A%E5%8F%98%E9%87%8Ffor-in%E5%BE%AA%E7%8E%AF/" id="post-next" class="post-nav-link">
        <div class="tips">Next <i class="icon icon-angle-right icon-lg icon-pl"></i></div>
        <h4 class="title">字典中存储列表及多变量for-in循环</h4>
      </a>
    </div>
  
</nav>



    




















</article>

<div id="reward" class="page-modal reward-lay">
    <a class="close" href="javascript:;"><i class="icon icon-close"></i></a>
    <h3 class="reward-title">
        <i class="icon icon-quote-left"></i>
        谢谢大爷~
        <i class="icon icon-quote-right"></i>
    </h3>
    <div class="reward-content">
        
        <div class="reward-code">
            <img id="rewardCode" src="/blog/img/wechat.jpg" alt="打赏二维码">
        </div>
        
        <label class="reward-toggle">
            <input id="rewardToggle" type="checkbox" class="reward-toggle-check"
                data-wechat="/blog/img/wechat.jpg" data-alipay="/blog/img/alipay.jpg">
            <div class="reward-toggle-ctrol">
                <span class="reward-toggle-item wechat">微信</span>
                <span class="reward-toggle-label"></span>
                <span class="reward-toggle-item alipay">支付宝</span>
            </div>
        </label>
        
    </div>
</div>



</div>

        <footer class="footer">
    <div class="top">
        
<p>
    <span id="busuanzi_container_site_uv" style='display:none'>
        站点总访客数：<span id="busuanzi_value_site_uv"></span>
    </span>
    <span id="busuanzi_container_site_pv" style='display:none'>
        站点总访问量：<span id="busuanzi_value_site_pv"></span>
    </span>
</p>


        <p>
            
            <span>This blog is licensed under a <a rel="license noopener" href="https://creativecommons.org/licenses/by/4.0/" target="_blank">Creative Commons Attribution 4.0 International License</a>.</span>
        </p>
    </div>
    <div class="bottom">
        <p><span>Song Wani &copy; 2019 - 2021</span>
            <span>
                
                Power by <a href="http://hexo.io/" target="_blank">Hexo</a> Theme <a href="https://github.com/yscoder/hexo-theme-indigo" target="_blank">indigo</a>
            </span>
        </p>
    </div>
</footer>

    </main>
    <div class="mask" id="mask"></div>
<a href="javascript:;" id="gotop" class="waves-effect waves-circle waves-light"><span class="icon icon-lg icon-chevron-up"></span></a>



<div class="global-share" id="globalShare">
    <ul class="reset share-icons">
      <li>
        <a class="weibo share-sns" target="_blank" href="http://service.weibo.com/share/share.php?url=https://gitee.com/songbug/blog.git/2020/07/16/%E5%8D%83%E5%B3%B0%E6%95%99%E7%A8%8B%E7%AC%94%E8%AE%B0/&title=《千峰教程笔记》 — SongKaBaKa&pic=https://gitee.com/songbug/blog.git/img/avatar.jpg" data-title="微博">
          <i class="icon icon-weibo"></i>
        </a>
      </li>
      <li>
        <a class="weixin share-sns wxFab" href="javascript:;" data-title="微信">
          <i class="icon icon-weixin"></i>
        </a>
      </li>
      <li>
        <a class="qq share-sns" target="_blank" href="http://connect.qq.com/widget/shareqq/index.html?url=https://gitee.com/songbug/blog.git/2020/07/16/%E5%8D%83%E5%B3%B0%E6%95%99%E7%A8%8B%E7%AC%94%E8%AE%B0/&title=《千峰教程笔记》 — SongKaBaKa&source=" data-title=" QQ">
          <i class="icon icon-qq"></i>
        </a>
      </li>
      <li>
        <a class="facebook share-sns" target="_blank" href="https://www.facebook.com/sharer/sharer.php?u=https://gitee.com/songbug/blog.git/2020/07/16/%E5%8D%83%E5%B3%B0%E6%95%99%E7%A8%8B%E7%AC%94%E8%AE%B0/" data-title=" Facebook">
          <i class="icon icon-facebook"></i>
        </a>
      </li>
      <li>
        <a class="twitter share-sns" target="_blank" href="https://twitter.com/intent/tweet?text=《千峰教程笔记》 — SongKaBaKa&url=https://gitee.com/songbug/blog.git/2020/07/16/%E5%8D%83%E5%B3%B0%E6%95%99%E7%A8%8B%E7%AC%94%E8%AE%B0/&via=https://gitee.com/songbug/blog.git" data-title=" Twitter">
          <i class="icon icon-twitter"></i>
        </a>
      </li>
      <li>
        <a class="google share-sns" target="_blank" href="https://plus.google.com/share?url=https://gitee.com/songbug/blog.git/2020/07/16/%E5%8D%83%E5%B3%B0%E6%95%99%E7%A8%8B%E7%AC%94%E8%AE%B0/" data-title=" Google+">
          <i class="icon icon-google-plus"></i>
        </a>
      </li>
    </ul>
 </div>


<div class="page-modal wx-share" id="wxShare">
    <a class="close" href="javascript:;"><i class="icon icon-close"></i></a>
    <p>扫一扫，分享到微信</p>
    <img src="//api.qrserver.com/v1/create-qr-code/?data=https://gitee.com/songbug/blog.git/2020/07/16/%E5%8D%83%E5%B3%B0%E6%95%99%E7%A8%8B%E7%AC%94%E8%AE%B0/" alt="微信分享二维码">
</div>




    <script src="//cdn.bootcss.com/node-waves/0.7.4/waves.min.js"></script>
<script>
var BLOG = { ROOT: '/blog/', SHARE: true, REWARD: true };


</script>

<script src="//unpkg.com/hexo-theme-material-indigo@latest/js/main.min.js"></script>


<div class="search-panel" id="search-panel">
    <ul class="search-result" id="search-result"></ul>
</div>
<template id="search-tpl">
<li class="item">
    <a href="{path}" class="waves-block waves-effect">
        <div class="title ellipsis" title="{title}">{title}</div>
        <div class="flex-row flex-middle">
            <div class="tags ellipsis">
                {tags}
            </div>
            <time class="flex-col time">{date}</time>
        </div>
    </a>
</li>
</template>

<script src="//unpkg.com/hexo-theme-material-indigo@latest/js/search.min.js" async></script>






<script async src="//busuanzi.ibruce.info/busuanzi/2.3/busuanzi.pure.mini.js"></script>



<script>
(function() {
    var OriginTitile = document.title, titleTime;
    document.addEventListener('visibilitychange', function() {
        if (document.hidden) {
            document.title = '死鬼去哪里了！';
            clearTimeout(titleTime);
        } else {
            document.title = '(つェ⊂)咦!又好了!';
            titleTime = setTimeout(function() {
                document.title = OriginTitile;
            },2000);
        }
    });
})();
</script>



</body>
</html>
